diff --git a/admin-threads.php b/admin-threads.php
index 326ac60..34f6a12 100644
--- a/admin-threads.php
+++ b/admin-threads.php
@@ -6,10 +6,10 @@
   require 'lib/layout.php';
 
   print "$header<br>";
-	
+
   if (!$isadmin) {
 
-	print "  
+	print "
 		$tblstart
 			$tccell1>This feature is restricted.</td>
 		$tblend
@@ -24,7 +24,7 @@
 	print adminlinkbar("admin-threads.php");
 
 	if (!$_POST['run']) {
-		print "<form action=\"admin-threads.php\" method=\"post\">  
+		print "<form action=\"admin-threads.php\" method=\"post\">
 			$tblstart
 				<tr>$tccellh>Thread Repair System</td></tr>
 				<tr>$tccell1>&nbsp;
@@ -57,7 +57,7 @@
 			</tr>
 		";
 
-	$q	= "SELECT `posts`.`thread`, (COUNT(`posts`.`id`)) AS 'real', ((COUNT(`posts`.`id`) - 1) - `threads`.`replies`) AS 'offset', `threads`.`replies`, `threads`.`title` AS `threadname`  FROM `posts` LEFT JOIN `threads` ON `posts`.`thread` = `threads`.`id` GROUP BY `thread` ORDER BY `offset` DESC";
+	$q	= "SELECT `posts`.`thread`, (COUNT(`posts`.`id`)) AS 'real', ((CAST(COUNT(`posts`.`id`) AS SIGNED) - 1) - CAST(`threads`.`replies` AS SIGNED)) AS 'offset', `threads`.`replies`, `threads`.`title` AS `threadname`  FROM `posts` LEFT JOIN `threads` ON `posts`.`thread` = `threads`.`id` GROUP BY `thread` HAVING `offset` <> 0 OR `offset` IS NULL ORDER BY ISNULL(`threadname`) ASC, `thread` DESC";
 	$sql	= mysql_query($q) or die(mysql_error());
 
 	$count	= "";
@@ -65,33 +65,28 @@
 
 		$status	= "";
 
-		if ($data['offset'] != 0) {
+		if ($data['offset'] != 0 || $data['offset'] === null) {
 
-			if ($data['offset'] >= 10000000) { 
-				$data['offset']	= ($data['real'] - 1) - $data['replies'];
-//				$status			= "<font color=\"#ff8080\">First post missing or otherwise broken</font>";
-//				$data['offset']	= "&nbsp;";
-			}
-
-			if (!$status) {
+			if ($data['replies'] === null) {
+				$status			= "<font color=\"#ff8080\">Invalid thread</font>";
+			} else {
 				$status	= mysql_query("UPDATE `threads` SET `replies` = '". ($data['real'] - 1) ."' WHERE `id` = '". $data['thread'] ."'") or "<font color=#ff0000>Error</font>: ". mysql_error();
 				if ($status == 1) $status	= "<font color=#80ff80>Updated</font>";
-//				$status	= "Not updated";
 				$count++;
 			}
 
 			print "
 			<tr>
-				$tccell1>". $data['thread'] ."</td>
-				$tccell2l><a href=\"thread.php?id=". $data['thread'] ."\">". $data['threadname'] ."</a></td>
-				$tccell1r>". $data['replies'] ."</td>
-				$tccell1r>". $data['real'] ."</td>
-				$tccell2r><b>". $data['offset'] ."</b></td>
-				$tccell1l>$status</td>
-			</tr>";		
+				$tccell1><a href=\"thread.php?id=". $data['thread'] ."\">". $data['thread'] ."</a></td>
+				$tccell2l><a href=\"thread.php?id=". $data['thread'] ."\">". ($data['threadname'] !== null ? $data['threadname'] : "<em>(Deleted thread)</em>") ."</a></td>
+				$tccell1>". ($data['replies'] !== null ? $data['replies'] + 1 : "&mdash;") ."</td>
+				$tccell1>". ($data['real']) ."</td>
+				$tccell2><b>". ($data['offset'] !== null ? $data['offset'] : "&mdash;") ."</b></td>
+				$tccell1>$status</td>
+			</tr>";
 
 		} else {
-			break;
+			continue;
 		}
 	}
 
@@ -106,9 +101,8 @@
 	}
  }
 
-  
+
   print "$tblend
 	$footer
 	";
   printtimedif($startingtime);
-?>
diff --git a/admin-threads2.php b/admin-threads2.php
index 8b9a2f6..b089a51 100644
--- a/admin-threads2.php
+++ b/admin-threads2.php
@@ -6,10 +6,10 @@
   require 'lib/layout.php';
 
   print "$header<br>";
-	
+
   if (!$isadmin) {
 
-	print "  
+	print "
 		$tblstart
 			$tccell1>This feature is restricted.</td>
 		$tblend
@@ -23,7 +23,7 @@
 	print adminlinkbar("admin-threads2.php");
 
 	if (!$_POST['run']) {
-		print "<form action=\"admin-threads2.php\" method=\"post\">  
+		print "<form action=\"admin-threads2.php\" method=\"post\">
 			$tblstart
 				<tr>$tccellh>Thread Repair System II</td></tr>
 				<tr>$tccell1>&nbsp;
@@ -57,8 +57,8 @@
 		";
 
 
- 
-	$q	= "SELECT `threads`.`id`, `threads`.`title` , `threads`.`lastpostdate` , `posts`.`date` as realdate, (`posts`.`date` - `threads`.`lastpostdate`) AS `diff` FROM `threads` LEFT JOIN (SELECT MAX(`date`) as `date`, `thread` FROM `posts` GROUP BY `thread`) as `posts`  ON `posts`.`thread` = `threads`.`id` ORDER BY `diff` DESC";
+
+	$q	= "SELECT `threads`.`id`, `threads`.`title` , `threads`.`lastpostdate` , `posts`.`date` as realdate FROM `threads` LEFT JOIN (SELECT MAX(`date`) as `date`, `thread` FROM `posts` GROUP BY `thread`) as `posts`  ON `posts`.`thread` = `threads`.`id` ORDER BY `threads`.`id` DESC";
 	$sql	= mysql_query($q) or die(mysql_error());
 
 	$count	= "";
@@ -68,7 +68,7 @@
 
 		if ($data['lastpostdate'] != $data['realdate']) {
 
-			if ($data['lastpostdate'] == "0" && $data['realdate'] == NULL) {
+			if ($data['lastpostdate'] == "0" && $data['realdate'] === null) {
 				$status	= "<font color=#ff8888>Broken thread</font>";
 			} else {
 
@@ -89,7 +89,7 @@
 				$tccell1>". ($data['realdate'] ? date($dateformat, $data['realdate'] + $tzoff) : "-") ."</td>
 				$tccell1>". timeunits2($data['lastpostdate'] - $data['realdate']) ."</td>
 				$tccell2l>$status</td>
-			</tr>";	
+			</tr>";
 		}
 	}
 
@@ -100,9 +100,8 @@
 	}
  }
 
-  
+
   print "$tblend
 	$footer
 	";
   printtimedif($startingtime);
-?>