--- modules/comment/comment.module	2008-02-02 06:28:06.000000000 +0000
+++ modules/comment/comment.module	2008-02-02 05:59:35.000000000 +0000
@@ -988,7 +988,6 @@
       // Start a form, for use with comment control.
       $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
 
-      $divs = 0;
       $num_rows = FALSE;
       $comments = '';
       drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
@@ -997,19 +996,6 @@
         $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
         $comment->depth = count(explode('.', $comment->thread)) - 1;
 
-        if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
-          if ($comment->depth > $divs) {
-            $divs++;
-            $comments .= '<div class="indented">';
-          }
-          else {
-            while ($comment->depth < $divs) {
-              $divs--;
-              $comments .= '</div>';
-            }
-          }
-        }
-
         if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
           $comments .= theme('comment_flat_collapsed', $comment, $node);
         }
@@ -1025,8 +1011,11 @@
 
         $num_rows = TRUE;
       }
-      while ($divs-- > 0) {
-        $comments .= '</div>';
+      if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
+        // De-indent to correct depth
+        $dummy->cid = NULL;
+        $dummy->depth = 0;
+        theme('comment_view', $dummy, $node, '');
       }
 
       $comment_controls = variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN);
@@ -1557,8 +1546,26 @@
  */
 function theme_comment_view($comment, $node, $links = array(), $visible = TRUE) {
   static $first_new = TRUE;
+  static $divs = 0;
+  static $mode;
 
   $output = '';
+  if (!isset($mode)) $mode = _comment_get_display_setting('mode', $node);
+  if ($mode == COMMENT_MODE_THREADED_COLLAPSED || $mode == COMMENT_MODE_THREADED_EXPANDED) {
+    if ($comment->depth > $divs) {
+      $divs++;
+      $output .= '<div class="indented">';
+    }
+    else {
+      while ($comment->depth < $divs) {
+        $divs--;
+        $output .= '</div>';
+      }
+    }
+  }
+  if (!isset($comment->cid)) {
+    return $output;
+  }
   $comment->new = node_mark($comment->nid, $comment->timestamp);
   if ($first_new && $comment->new != MARK_READ) {
     // Assign the anchor only for the first new comment. This avoids duplicate
