diff --git a/include/class.orm.php b/include/class.orm.php
index ae82a7911a9831b875d1568b3dbdd7fcc5dd4f9e..53970b9d6fe14547b5929a34c191cc16bf70ad70 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -124,8 +124,10 @@ class ModelMeta implements ArrayAccess {
     function extend(ModelMeta $child, $meta) {
         $this->subclasses[$child->model] = $child;
         // Merge 'joins' settings (instead of replacing)
-        if (isset($this->meta['joins']))
-            $meta['joins'] += $this->meta['joins'];
+        if (isset($this->meta['joins'])) {
+            $meta['joins'] = array_merge($meta['joins'] ?: array(),
+                $this->meta['joins']);
+        }
         return $meta + $this->meta + self::$base;
     }
 
@@ -1358,6 +1360,9 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
      * after this is run, the changes should be made in a clone.
      */
     function total() {
+        if (isset($this->total))
+            return $this->total;
+
         // Optimize the query with the CALC_FOUND_ROWS if
         // - the compiler supports it
         // - the iterator hasn't yet been built, that is, the query for this
diff --git a/include/staff/templates/queue-tickets.tmpl.php b/include/staff/templates/queue-tickets.tmpl.php
index d7a180bbc5190dc57f882c72b6b1c53b66ee2e9e..212f725410fc3649063ee4ffa5ddad3ad10dc5d7 100644
--- a/include/staff/templates/queue-tickets.tmpl.php
+++ b/include/staff/templates/queue-tickets.tmpl.php
@@ -72,6 +72,29 @@ elseif ($queue_sort = $queue->getDefaultSort()) {
         'dir' => (int) $_GET['dir'] ?: 0,
     );
 }
+
+// Handle current sorting preferences
+
+$sorted = false;
+foreach ($columns as $C) {
+    // Sort by this column ?
+    if (isset($sort['col']) && $sort['col'] == $C->id) {
+        $tickets = $C->applySort($tickets, $sort['dir']);
+        $sorted = true;
+    }
+}
+if (!$sorted && isset($sort['queuesort'])) {
+    // Apply queue sort-dropdown selected preference
+    $sort['queuesort']->applySort($tickets, $sort['dir']);
+}
+
+// Apply pagination
+
+$page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
+$count = $tickets->total();
+$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
+$pageNav->setURL('tickets.php', $args);
+$tickets = $pageNav->paginate($tickets);
 ?>
 
 <!-- SEARCH FORM START -->
@@ -190,8 +213,6 @@ if ($canManageTickets) { ?>
         <th style="width:12px"></th>
 <?php 
 }
-
-$sorted = false;
 foreach ($columns as $C) {
     $heading = Format::htmlchars($C->getLocalHeading());
     if ($C->isSortable()) {
@@ -204,27 +225,12 @@ foreach ($columns as $C) {
     }
     echo sprintf('<th width="%s" data-id="%d">%s</th>',
         $C->getWidth(), $C->id, $heading);
-
-    // Sort by this column ?
-    if (isset($sort['col']) && $sort['col'] == $C->id) {
-        $tickets = $C->applySort($tickets, $sort['dir']);
-        $sorted = true;
-    }
-}
-if (!$sorted && isset($sort['queuesort'])) {
-    $sort['queuesort']->applySort($tickets, $sort['dir']);
 }
 ?>
     </tr>
   </thead>
   <tbody>
 <?php
-$page = ($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
-$count = $tickets->total();
-$pageNav = new Pagenate($count, $page, PAGE_LIMIT);
-$pageNav->setURL('tickets.php', $args);
-$tickets = $pageNav->paginate($tickets);
-
 foreach ($tickets as $T) {
     echo '<tr>';
     if ($canManageTickets) { ?>
diff --git a/scp/tickets.php b/scp/tickets.php
index 783a27830c8f411892dae17cb497bb04227a1afd..23475ee9f26b66618ea282816e29fed9e087ed19 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -385,7 +385,9 @@ if($_POST && !$errors):
                              __('Contact admin for such access'));
                 } else {
                     $vars = $_POST;
-                    $vars['uid'] = $user? $user->getId() : 0;
+
+                    if ($vars['uid'] && (!User::lookup($vars['uid'])))
+                        $vars['uid'] = 0;
 
                     $vars['cannedattachments'] = $response_form->getField('attachments')->getClean();