--- modules/comment/comment.module	2008-01-31 23:29:03.000000000 +0000
+++ /home/nick/drupal/modules/comment/comment.module	2008-02-01 22:04:35.000000000 +0000
@@ -988,7 +988,7 @@
       // Start a form, for use with comment control.
       $result = pager_query($query, $comments_per_page, 0, $query_count, $query_args);
 
-      $divs = 0;
+      $depth = $divs = 0;
       $num_rows = FALSE;
       $comments = '';
       drupal_add_css(drupal_get_path('module', 'comment') .'/comment.css');
@@ -998,36 +998,27 @@
         $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>';
-            }
-          }
+          $depth = $comment->depth - $divs;
+          $divs = $comment->depth;
         }
 
         if ($mode == COMMENT_MODE_FLAT_COLLAPSED) {
-          $comments .= theme('comment_flat_collapsed', $comment, $node);
+          $comments .= theme('comment_flat_collapsed', $comment, $node, $depth);
         }
         else if ($mode == COMMENT_MODE_FLAT_EXPANDED) {
-          $comments .= theme('comment_flat_expanded', $comment, $node);
+          $comments .= theme('comment_flat_expanded', $comment, $node, $depth);
         }
         else if ($mode == COMMENT_MODE_THREADED_COLLAPSED) {
-          $comments .= theme('comment_thread_collapsed', $comment, $node);
+          $comments .= theme('comment_thread_collapsed', $comment, $node, $depth);
         }
         else if ($mode == COMMENT_MODE_THREADED_EXPANDED) {
-          $comments .= theme('comment_thread_expanded', $comment, $node);
+          $comments .= theme('comment_thread_expanded', $comment, $node, $depth);
         }
 
         $num_rows = TRUE;
       }
-      while ($divs-- > 0) {
-        $comments .= '</div>';
-      }
+      // De-indent to correct depth
+      theme('comment_view', null, $node, '', -$divs);
 
       $comment_controls = variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN);
       if ($num_rows && ($comment_controls == COMMENT_CONTROLS_ABOVE || $comment_controls == COMMENT_CONTROLS_ABOVE_BELOW)) {
@@ -1551,14 +1542,26 @@
  *   The comment node.
  * @param $links
  *   An associative array containing control links.
+ * @param $depth
+ *   depth of indentation (+1: indent, -n: de-indent n levels).
  * @param $visible
  *   Switches between folded/unfolded view.
  * @ingroup themeable
  */
-function theme_comment_view($comment, $node, $links = array(), $visible = TRUE) {
+function theme_comment_view($comment, $node, $links = array(), $visible = TRUE, $depth = 0) {
   static $first_new = TRUE;
 
   $output = '';
+  if (0 < $depth) {
+    $output .= '<div class="indented">';
+  } else {
+    while ($depth++ < 0) {
+      $output .= '</div>';
+    }
+  }
+  if (!isset($comment)) {
+    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
@@ -1716,10 +1719,12 @@
  *   The comment to be themed.
  * @param $node
  *   The comment node.
+ * @param $depth
+ *   depth of indentation (+1: indent, -n: de-indent n levels).
  * @ingroup themeable
  */
-function theme_comment_flat_collapsed($comment, $node) {
-  return theme('comment_view', $comment, $node, '', 0);
+function theme_comment_flat_collapsed($comment, $node, $depth = 0) {
+  return theme('comment_view', $comment, $node, '', 0, $depth);
 }
 
 /**
@@ -1729,10 +1734,12 @@
  *   The comment to be themed.
  * @param $node
  *   The comment node.
+ * @param $depth
+ *   depth of indentation (+1: indent, -n: de-indent n levels).
  * @ingroup themeable
  */
-function theme_comment_flat_expanded($comment, $node) {
-  return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0));
+function theme_comment_flat_expanded($comment, $node, $depth = 0) {
+  return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0), TRUE, $depth);
 }
 
 /**
@@ -1742,10 +1749,12 @@
  *   The comment to be themed.
  * @param $node
  *   The comment node.
+ * @param $depth
+ *   depth of indentation (+1: indent, -n: de-indent n levels).
  * @ingroup themeable
  */
-function theme_comment_thread_collapsed($comment, $node) {
-  return theme('comment_view', $comment, $node, '', 0);
+function theme_comment_thread_collapsed($comment, $node, $depth = 0) {
+  return theme('comment_view', $comment, $node, '', 0, $depth);
 }
 
 /**
@@ -1755,10 +1764,12 @@
  *   The comment to be themed.
  * @param $node
  *   The comment node.
+ * @param $depth
+ *   depth of indentation (+1: indent, -n: de-indent n levels).
  * @ingroup themeable
  */
-function theme_comment_thread_expanded($comment, $node) {
-  return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0));
+function theme_comment_thread_expanded($comment, $node, $depth = 0) {
+  return theme('comment_view', $comment, $node, module_invoke_all('link', 'comment', $comment, 0), TRUE, $depth);
 }
 
 /**
