diff --git a/include/class.orm.php b/include/class.orm.php
index 861a40b14843f712fc13b80543f690e6a0418e91..fbbdf7f1127f930f2221402b3a055e8cbe712021 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -1484,6 +1484,50 @@ implements IteratorAggregate, Countable, ArrayAccess {
         $this->asArray();
         return count($this->cache);
     }
+
+    /**
+     * Sort the instrumented list in place. This would be useful to change the
+     * sorting order of the items in the list without fetching the list from
+     * the database again.
+     *
+     * Parameters:
+     * $key - (callable|int) A callable function to produce the sort keys
+     *      or one of the SORT_ constants used by the array_multisort
+     *      function
+     * $reverse - (bool) true if the list should be sorted descending
+     *
+     * Returns:
+     * This instrumented list for chaining and inlining.
+     */
+    function sort($key=false, $reverse=false) {
+        // Fetch all records into the cache
+        $this->asArray();
+        if (is_callable($key)) {
+            array_multisort(
+                array_map($key, $this->cache),
+                $reverse ? SORT_DESC : SORT_ASC,
+                $this->cache);
+        }
+        elseif ($key) {
+            array_multisort($this->cache,
+                $reverse ? SORT_DESC : SORT_ASC, $key);
+        }
+        elseif ($reverse) {
+            rsort($this->cache);
+        }
+        else
+            sort($this->cache);
+        return $this;
+    }
+
+    /**
+     * Reverse the list item in place. Returns this object for chaining
+     */
+    function reverse() {
+        $this->asArray();
+        array_reverse($this->cache);
+        return $this;
+    }
 }
 
 class ModelResultSet
@@ -1880,50 +1924,6 @@ extends ModelResultSet {
         return new static(array($this->model, $key), $this->filter($constraint));
     }
 
-    /**
-     * Sort the instrumented list in place. This would be useful to change the
-     * sorting order of the items in the list without fetching the list from
-     * the database again.
-     *
-     * Parameters:
-     * $key - (callable|int) A callable function to produce the sort keys
-     *      or one of the SORT_ constants used by the array_multisort
-     *      function
-     * $reverse - (bool) true if the list should be sorted descending
-     *
-     * Returns:
-     * This instrumented list for chaining and inlining.
-     */
-    function sort($key=false, $reverse=false) {
-        // Fetch all records into the cache
-        $this->asArray();
-        if (is_callable($key)) {
-            array_multisort(
-                array_map($key, $this->cache),
-                $reverse ? SORT_DESC : SORT_ASC,
-                $this->cache);
-        }
-        elseif ($key) {
-            array_multisort($this->cache,
-                $reverse ? SORT_DESC : SORT_ASC, $key);
-        }
-        elseif ($reverse) {
-            rsort($this->cache);
-        }
-        else
-            sort($this->cache);
-        return $this;
-    }
-
-    /**
-     * Reverse the list item in place. Returns this object for chaining
-     */
-    function reverse() {
-        $this->asArray();
-        array_reverse($this->cache);
-        return $this;
-    }
-
     // Save all changes made to any list items
     function saveAll() {
         foreach ($this as $I)
@@ -2908,7 +2908,6 @@ class MySqlPreparedExecutor {
     // queries
     var $map;
 
-    var $conn;
     var $unbuffered = false;
 
     function __construct($sql, $params, $map=null) {
@@ -2923,10 +2922,6 @@ class MySqlPreparedExecutor {
 
     function setBuffered($buffered) {
         $this->unbuffered = !$buffered;
-        if (!$buffered) {
-            // Execute this query in another session
-            $this->conn = Bootstrap::connect();
-        }
     }
 
     function fixupParams() {
@@ -2947,9 +2942,9 @@ class MySqlPreparedExecutor {
 
     function execute() {
         list($sql, $params) = $this->fixupParams();
-        if (!($this->stmt = db_prepare($sql, $this->conn)))
+        if (!($this->stmt = db_prepare($sql)))
             throw new InconsistentModelException(
-                'Unable to prepare query: '.db_error($this->conn).' '.$sql);
+                'Unable to prepare query: '.db_error().' '.$sql);
         if (count($params))
             $this->_bind($params);
         if (!$this->stmt->execute() || !($this->unbuffered || $this->stmt->store_result())) {
@@ -3072,6 +3067,9 @@ class MySqlPreparedExecutor {
             if ($p instanceof DateTime) {
                 $p = $p->format('Y-m-d H:i:s');
             }
+            elseif ($p === false) {
+                $p = 0;
+            }
             return db_real_escape($p, is_string($p));
         }, $this->sql);
     }
diff --git a/include/client/templates/thread-entries.tmpl.php b/include/client/templates/thread-entries.tmpl.php
index b91b91d74ccb7a32d7949e3ce64f3a1eb2ea7db1..1f2c1eed4e3144259475b76b1dd9054dbeded451 100644
--- a/include/client/templates/thread-entries.tmpl.php
+++ b/include/client/templates/thread-entries.tmpl.php
@@ -2,7 +2,7 @@
 $events = $events
     ->filter(array('state__in' => array('created', 'closed', 'reopened', 'edited', 'collab')))
     ->order_by('id');
-$events = $events->getIterator();
+$events = new IteratorIterator($events->all());
 $events->rewind();
 $event = $events->current();
 
diff --git a/include/mysqli.php b/include/mysqli.php
index 4ab5e8cf39300d757d3e247f22a8092d795248b8..41e7f1fe070ad0a2f32e802ead5acae1b48f6eba 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -77,8 +77,6 @@ function db_connect($host, $user, $passwd, $options = array()) {
     ), 'session');
     $__db->set_charset('utf8');
 
-    @db_set_variable('sql_mode', '');
-
     $__db->autocommit(true);
 
     // Use connection timing to seed the random number generator
@@ -140,12 +138,12 @@ function db_set_all($variables, $type='session') {
         if (in_array($k, ['NAMES', 'CHARACTER SET'])) {
             // MySQL doesn't support the session/global flag, and doesn't
             // use an equal sign for these
-            $type = '';
+            $T = '';
         }
         else {
-            $k .= ' = ';
+            $k .= ' =';
         }
-        $set[] = "$type $k ".($__db->real_escape_string($v) ?: "''");
+        $set[] = "$T $k ".($__db->real_escape_string($v) ?: "''");
     }
     $sql = 'SET ' . implode(', ', $set);
     return db_query($sql);
diff --git a/include/staff/faq-categories.inc.php b/include/staff/faq-categories.inc.php
index d2aeb0cb7bc2da4d6c0a5cd19115a9eba5a3e479..e90f06cd88017ce603f952c76eebf535c561ff6c 100644
--- a/include/staff/faq-categories.inc.php
+++ b/include/staff/faq-categories.inc.php
@@ -144,9 +144,7 @@ if($_REQUEST['q'] || $_REQUEST['cid'] || $_REQUEST['topicId']) { //Search.
         ->all();
 
     if (count($categories)) {
-        usort($categories, function($a, $b) {
-            return strcmp($a->getLocalName(), $b->getLocalName());
-        });
+        $categories->sort(function($a) { return $a->getLocalName(); });
         echo '<div>'.__('Click on the category to browse FAQs or manage its existing FAQs.').'</div>
                 <ul id="kb">';
         foreach ($categories as $C) {
diff --git a/include/staff/templates/thread-entries.tmpl.php b/include/staff/templates/thread-entries.tmpl.php
index cabd7560b722df841904cfda1398f87bd93eb0ab..8786472e5f16b52f59eb3e59857254ee3c8e1a3f 100644
--- a/include/staff/templates/thread-entries.tmpl.php
+++ b/include/staff/templates/thread-entries.tmpl.php
@@ -10,7 +10,7 @@ $cmp = function ($a, $b) use ($sort) {
 };
 
 $events = $events->order_by($sort);
-$events = $events->getIterator();
+$events = new IteratorIterator($events->all());
 $events->rewind();
 $event = $events->current();
 $htmlId = $options['html-id'] ?: ('thread-'.$this->getId());