diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 2a176b9b8c7699010639d54d6dadadb19af36488..be1d0b57e3bdd0e11d37ef6bfb3cfae14772e87b 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -1456,7 +1456,7 @@ function refer($tid, $target=null) {
                 && ($f=$iform->getField('duedate'))) {
             $f->configure('max', Misc::db2gmtime($ticket->getEstDueDate()));
         }
-        $vars = array_merge($_SESSION[':form-data'], $vars);
+        $vars = array_merge($_SESSION[':form-data'] ? : array(), $vars);
 
         if ($_POST) {
             Draft::deleteForNamespace(
diff --git a/include/class.forms.php b/include/class.forms.php
index 3cef29c99e95e1dfd1b862b75d46d54bc647faed..45a3b3bf1cc852570a77d61e8a81309933a3e4b5 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2672,6 +2672,27 @@ class ThreadEntryField extends FormField {
 }
 
 class PriorityField extends ChoiceField {
+
+    var $priorities;
+    var $_choices;
+
+    function getPriorities() {
+        if (!isset($this->priorities))
+            $this->priorities = Priority::objects();
+
+        return $this->priorities;
+    }
+
+    function getPriority($id) {
+
+        if ($this->getPriorities() &&
+                ($p=$this->priorities->findFirst(array('priority_id' =>
+                                                       $id))))
+            return $p;
+
+        return Priority::lookup($id);
+    }
+
     function getWidget($widgetClass=false) {
         $widget = parent::getWidget($widgetClass);
         if ($widget->value instanceof Priority)
@@ -2684,15 +2705,15 @@ class PriorityField extends ChoiceField {
     }
 
     function getChoices($verbose=false) {
-        $sql = 'SELECT priority_id, priority_desc FROM '.PRIORITY_TABLE
-              .' ORDER BY priority_urgency DESC';
-        $choices = array('' => '— '.__('Default').' —');
-        if (!($res = db_query($sql)))
-            return $choices;
-
-        while ($row = db_fetch_row($res))
-            $choices[$row[0]] = $row[1];
-        return $choices;
+
+        if (!isset($this->_choices)) {
+            $choices = array('' => '— '.__('Default').' —');
+            foreach ($this->getPriorities() as $p)
+                $choices[$p->getId()] = $p->getDesc();
+            $this->_choices = $choices;
+        }
+
+        return $this->_choices;
     }
 
     function parse($id) {
@@ -2710,8 +2731,8 @@ class PriorityField extends ChoiceField {
             list($value, $id) = $value;
         elseif ($id === false)
             $id = $value;
-        if ($id)
-            return Priority::lookup($id);
+
+        return $this->getPriority($id);
     }
 
     function to_database($prio) {
diff --git a/include/class.osticket.php b/include/class.osticket.php
index db48388e03dc852c9a9ddc06ffe9c9bf7a030211..b53e849f4574eb257870b0003a595314484c6ca7 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -59,7 +59,6 @@ class osTicket {
         if (!defined('DISABLE_SESSION') || !DISABLE_SESSION)
             $this->session = osTicketSession::start(SESSION_TTL); // start DB based session
 
-        $this->config = new OsticketConfig();
 
         $this->csrf = new CSRF('__CSRFToken__');
 
@@ -85,6 +84,9 @@ class osTicket {
     }
 
     function getConfig() {
+        if (!isset($this->config))
+            $this->config = new OsticketConfig();
+
         return $this->config;
     }
 
@@ -436,7 +438,7 @@ class osTicket {
             switch ($info['v']) {
             case '1':
                 if ($major && $info['m'] && $info['m'] != $major)
-                    continue;
+                    continue 2;
                 if ($product == 'core' && GIT_VERSION == '$git')
                     return $info['c'];
                 return $info['V'];
diff --git a/include/class.ostsession.php b/include/class.ostsession.php
index 439c745c2b7d898eb26216683c444da2db28eb22..55c12493297b1a803810526c6dd09bf9655d993f 100644
--- a/include/class.ostsession.php
+++ b/include/class.ostsession.php
@@ -292,12 +292,13 @@ extends SessionBackend {
                 if ($data = $this->memcache->get($key))
                     break;
             }
+
         }
 
         // No session data on record -- new session
         $this->isnew = $data === false;
 
-        return $data;
+        return $data ?: '';
     }
 
     function update($id, $data) {
@@ -311,6 +312,9 @@ extends SessionBackend {
             if (!$this->memcache->replace($key, $data, 0, $this->getTTL()));
                 $this->memcache->set($key, $data, 0, $this->getTTL());
         }
+
+        return true;
+
     }
 
     function destroy($id) {
@@ -321,6 +325,8 @@ extends SessionBackend {
             $this->memcache->replace($key, '', 0, 1);
             $this->memcache->delete($key, 0);
         }
+
+        return true;
     }
 
     function gc($maxlife) {
diff --git a/include/class.queue.php b/include/class.queue.php
index 901fadbab3bc275178a8fb377f3e4c43fda68ef0..8a39e0f55b9148adf9b1262973c6f79b6744332b 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -905,6 +905,7 @@ class CustomQueue extends VerySimpleModel {
 
         // Apply column, annotations and conditions additions
         foreach ($this->getColumns() as $C) {
+            $C->setQueue($this);
             $query = $C->mangleQuery($query, $this->getRoot());
         }
         return $query;
@@ -2144,7 +2145,7 @@ extends VerySimpleModel {
         if (!isset($this->_queue)) {
             $queue = $this->queue;
 
-            if (!$queue && ($queue_id = $this->queue_id))
+            if (!$queue && ($queue_id = $this->queue_id) && is_numeric($queue_id))
                 $queue = CustomQueue::lookup($queue_id);
 
             $this->_queue = $queue;
diff --git a/include/class.search.php b/include/class.search.php
index ac7a8df2d1bd2fcdfe5024651b4b64c923b312e4..7f39725668c970e8fd57e69ac7f2afd381a7c625 100755
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -483,7 +483,7 @@ class MysqlSearchBackend extends SearchBackend {
             LEFT JOIN `".TABLE_PREFIX."_search` A2 ON (A1.`id` = A2.`object_id` AND A2.`object_type`='H')
             WHERE A2.`object_id` IS NULL AND (A1.poster <> 'SYSTEM')
             AND (LENGTH(A1.`title`) + LENGTH(A1.`body`) > 0)
-            LIMIT 500";
+            ORDER BY A1.`id` DESC LIMIT 500";
         if (!($res = db_query_unbuffered($sql, $auto_create)))
             return false;
 
@@ -503,7 +503,7 @@ class MysqlSearchBackend extends SearchBackend {
         $sql = "SELECT A1.`ticket_id` FROM `".TICKET_TABLE."` A1
             LEFT JOIN `".TABLE_PREFIX."_search` A2 ON (A1.`ticket_id` = A2.`object_id` AND A2.`object_type`='T')
             WHERE A2.`object_id` IS NULL
-            LIMIT 300";
+            ORDER BY A1.`ticket_id` DESC LIMIT 300";
         if (!($res = db_query_unbuffered($sql, $auto_create)))
             return false;
 
@@ -897,6 +897,7 @@ class SavedQueue extends CustomQueue {
         if ($criteria && is_array($criteria))
             $queues->filter($criteria);
 
+       $counts = array();
         $query = Ticket::objects();
         // Apply tickets visibility for the agent
         $query = $agent->applyVisibility($query);
@@ -910,6 +911,8 @@ class SavedQueue extends CustomQueue {
 
             // Add extra tables joins  (if any)
             if ($Q->extra && isset($Q->extra['tables'])) {
+               $counts['q'.$queue->getId()] = 500;
+               continue;
                 $contraints = array();
                 if ($Q->constraints)
                      $constraints = new Q($Q->constraints);
@@ -919,7 +922,7 @@ class SavedQueue extends CustomQueue {
         }
 
         try {
-            $counts = $query->values()->one();
+            $counts = array_merge($counts, $query->values()->one());
         }  catch (Exception $ex) {
             foreach ($queues as $q)
                 $counts['q'.$q->getId()] = $q->getTotal();
diff --git a/include/class.staff.php b/include/class.staff.php
index 2618730bcb985800fb3a46766af83738232e089c..2b0471a4cabca3f7c254482ce1f0fcd878e1b0c9 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -69,16 +69,17 @@ implements AuthenticatedUser, EmailContact, TemplateVariable, Searchable {
 
     function get($field, $default=false) {
 
+       // Check primary fields
+        try {
+            return parent::get($field, $default);
+        } catch (Exception $e) {}
+
         // Autoload config if not loaded already
         if (!isset($this->_config))
             $this->getConfig();
 
         if (isset($this->_config[$field]))
             return $this->_config[$field];
-
-        try {
-            return parent::get($field, $default);
-        } catch (Exception $e) {}
     }
 
     function getConfig() {
@@ -476,10 +477,13 @@ implements AuthenticatedUser, EmailContact, TemplateVariable, Searchable {
         if (is_null($dept))
             return $this->role;
 
-        if ((!$dept instanceof Dept) && !($dept=Dept::lookup($dept)))
-            return null;
+       if (is_numeric($dept))
+          $deptId = $dept;
+       elseif($dept instanceof Dept)
+          $deptId = $dept->getId();
+       else
+          return null;
 
-        $deptId = $dept->getId();
         $roles = $this->getRoles();
         if (isset($roles[$deptId]))
             return $roles[$deptId];
diff --git a/include/class.thread.php b/include/class.thread.php
index 55be97a108a5fd22eca16543a87df79eab9d2848..7aae4f939bc5dec4ca298b7f1e698f314b556c41 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -228,7 +228,7 @@ implements Searchable {
 
             foreach ($vars['cid'] as $c) {
               $collab = Collaborator::lookup($c);
-              if(get_class($collab) == 'Collaborator') {
+              if (($collab instanceof Collaborator)) {
                 $collab->setFlag(Collaborator::FLAG_ACTIVE, true);
                 $collab->save();
               }
diff --git a/include/cli/modules/i18n.php b/include/cli/modules/i18n.php
index a20c8329db306d65fb284e3f0f29c62a7f71b62a..733bcb68c4a7be9e13511546af588ada9d75665b 100644
--- a/include/cli/modules/i18n.php
+++ b/include/cli/modules/i18n.php
@@ -417,7 +417,7 @@ class i18n_Compiler extends Module {
                     break;
                 case T_WHITESPACE:
                     // noop
-                    continue;
+                    continue 2;
                 case T_STRING_VARNAME:
                 case T_NUM_STRING:
                 case T_ENCAPSED_AND_WHITESPACE:
@@ -472,7 +472,7 @@ class i18n_Compiler extends Module {
         while (list(,$T) = each($tokens)) {
             switch ($T[0]) {
             case T_WHITESPACE:
-                continue;
+                continue 2;
             case '(':
                 return $this->__read_args($tokens, $args);
             default:
@@ -500,7 +500,7 @@ class i18n_Compiler extends Module {
                     break;
                 }
                 if (!isset($funcs[$T[1]]))
-                    continue;
+                    continue 2;
                 $constants = $funcs[$T[1]];
                 if ($info = $this->__get_func_args($tokens, $constants))
                     $T_funcs[] = $info;
@@ -519,7 +519,7 @@ class i18n_Compiler extends Module {
                         case '@trans':
                             $translate = true;
                         default:
-                            continue;
+                            continue 2;
                         }
                     }
                 }
diff --git a/setup/test/tests/class.php_analyze.php b/setup/test/tests/class.php_analyze.php
index 1ed22fe1a90dbac3303953f1ab01dbd9f53c866c..7635cf237ef54c5cab548db90b1a2ad2039e17f8 100644
--- a/setup/test/tests/class.php_analyze.php
+++ b/setup/test/tests/class.php_analyze.php
@@ -75,7 +75,7 @@ class SourceAnalyzer extends Test {
             list(,$token) = each($this->tokens);
             switch ($token[0]) {
             case T_WHITESPACE:
-                continue;
+                continue 2;
             case T_STRING:
                 $function['name'] = $token[1];
                 break;