diff --git a/advanced_forum.module b/advanced_forum.module
index 081d857..cdf62e4 100644
--- a/advanced_forum.module
+++ b/advanced_forum.module
@@ -1292,12 +1292,17 @@
 /**
  * Returns the display position of a given reply post ID on a given node.
  */
-function advanced_forum_post_position($node_id, $post_id) {
+function advanced_forum_post_position($node, $comment) {
   static $post_order = array();
+
+  $node_id = $node->nid;
+  $post_id = $comment->pid;

   if (!isset($post_order[$node_id])) {
     // Initialize the spot for this node's list.
     $post_order[$node_id] = array();
+
+    $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);

     // Get the list of CIDs from the database in order of oldest first.
     // We are going to make that assumption for now for simplicity but may
@@ -1305,9 +1310,18 @@
     $query = db_select('comment', 'c')
         ->fields('c', array('cid'))
         ->condition('nid', $node_id)
-        ->orderBy('cid', 'ASC')
-        ->execute();
+        ->addTag('node_access')
+        ->addTag('comment_filter');

+    if ($mode === COMMENT_MODE_FLAT) {
+      $query->orderBy('c.cid', 'ASC');
+    }
+    else {
+      $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
+      $query->orderBy('torder', 'ASC');
+    }
+
+    $query = $query->execute();
     // Cycle through the results and fill in the array.
     while ($post = $query->fetchAssoc()) {
       $post_order[$node_id][] = reset($post);
diff --git a/includes/advanced_forum_preprocess_comment.inc b/includes/advanced_forum_preprocess_comment.inc
index 588efe0..feea77e 100644
--- a/includes/advanced_forum_preprocess_comment.inc
+++ b/includes/advanced_forum_preprocess_comment.inc
@@ -86,7 +86,7 @@
   $variables['in_reply_to'] = "";
   if ($comment->pid > 0) {
     // Find the display position of the parent post;.
-    $post_position = advanced_forum_post_position($node->nid, $comment->pid);
+    $post_position = advanced_forum_post_position($node, $comment);

     // This extra if test is a sanity check in case the comment being replied
     // to no longer exists.
