diff --git a/gtk/gtkscrolledwindow.c b/gtk/gtkscrolledwindow.c
index 489d687..16a9e19 100644
--- a/gtk/gtkscrolledwindow.c
+++ b/gtk/gtkscrolledwindow.c
@@ -1444,6 +1444,43 @@ gtk_scrolled_window_relative_allocation (GtkWidget     *widget,
     }
 }
 
+static gboolean
+scrollbar_needed (GtkScrolledWindow *swindow,
+		  GtkOrientation     orientation)
+{
+  GtkScrolledWindowPrivate *priv = swindow->priv;
+  GtkAdjustment            *adjustment;
+
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    {
+      switch (priv->hscrollbar_policy)
+	{
+	case GTK_POLICY_ALWAYS:
+	  return TRUE;
+	case GTK_POLICY_NEVER:
+	  return FALSE;
+	case GTK_POLICY_AUTOMATIC:
+	  adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
+	  return (adjustment->upper - adjustment->lower > adjustment->page_size);
+	}
+    }
+  else /* GTK_ORIENTATION_VERTICAL */
+    {
+      switch (priv->vscrollbar_policy)
+	{
+	case GTK_POLICY_ALWAYS:
+	  return TRUE;
+	case GTK_POLICY_NEVER:
+	  return FALSE;
+	case GTK_POLICY_AUTOMATIC:
+	  adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
+	  return (adjustment->upper - adjustment->lower > adjustment->page_size);
+	}
+    }
+
+  return FALSE;
+}
+
 static void
 gtk_scrolled_window_size_allocate (GtkWidget     *widget,
 				   GtkAllocation *allocation)
@@ -1477,14 +1514,8 @@ gtk_scrolled_window_size_allocate (GtkWidget     *widget,
 
   gtk_widget_set_allocation (widget, allocation);
 
-  if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
-    priv->hscrollbar_visible = TRUE;
-  else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
-    priv->hscrollbar_visible = FALSE;
-  if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
-    priv->vscrollbar_visible = TRUE;
-  else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
-    priv->vscrollbar_visible = FALSE;
+  priv->hscrollbar_visible = scrollbar_needed (scrolled_window, GTK_ORIENTATION_HORIZONTAL);
+  priv->vscrollbar_visible = scrollbar_needed (scrolled_window, GTK_ORIENTATION_VERTICAL);
 
   child = gtk_bin_get_child (bin);
   if (child && gtk_widget_get_visible (child))
@@ -1495,6 +1526,9 @@ gtk_scrolled_window_size_allocate (GtkWidget     *widget,
       gboolean previous_vvis;
       guint count = 0;
 
+      previous_hvis = priv->hscrollbar_visible;
+      previous_vvis = priv->vscrollbar_visible;
+
       /* In the case that both scrollbars are visible in the previous round,
        * we dont do our guess-work before hand because it's possible some
        * infinite recursion was detected (leave it up to the child scrollable
@@ -1642,11 +1676,11 @@ gtk_scrolled_window_size_allocate (GtkWidget     *widget,
 	  child_allocation.width = relative_allocation.width;
 	  child_allocation.height = relative_allocation.height;
 
-	  previous_hvis = priv->hscrollbar_visible;
-	  previous_vvis = priv->vscrollbar_visible;
-
 	  gtk_widget_size_allocate (child, &child_allocation);
 
+	  priv->hscrollbar_visible = scrollbar_needed (scrolled_window, GTK_ORIENTATION_HORIZONTAL);
+	  priv->vscrollbar_visible = scrollbar_needed (scrolled_window, GTK_ORIENTATION_VERTICAL);
+
 	  /* If, after the first iteration, the hscrollbar and the
 	   * vscrollbar flip visiblity, then we need both.
 	   */
@@ -1663,6 +1697,9 @@ gtk_scrolled_window_size_allocate (GtkWidget     *widget,
 	      return;
 	    }
 	  
+	  previous_hvis = priv->hscrollbar_visible;
+	  previous_vvis = priv->vscrollbar_visible;
+
 	  count++;
 	}
       while (previous_hvis != priv->hscrollbar_visible ||
@@ -1861,9 +1898,7 @@ gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
 	{
 	  gboolean visible;
 
-	  visible = priv->hscrollbar_visible;
-	  priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
-					      adjustment->page_size);
+	  visible = (adjustment->upper - adjustment->lower > adjustment->page_size);
 	  if (priv->hscrollbar_visible != visible)
 	    gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
 	}
@@ -1875,9 +1910,7 @@ gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
 	{
 	  gboolean visible;
 
-	  visible = priv->vscrollbar_visible;
-	  priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
-					      adjustment->page_size);
+	  visible = (adjustment->upper - adjustment->lower > adjustment->page_size);
 	  if (priv->vscrollbar_visible != visible)
 	    gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
 	}
