diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 7333992a47726d49b4cbd60eb64e682b3cea9287..3496f2d517c0329d9275b8dbb75abdb9de3f1f86 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -204,7 +204,7 @@ class MailFetcher {
                     $s_filter = stream_filter_append($f, 'convert.base64-decode',STREAM_FILTER_WRITE);
                     if (!fwrite($f, $text))
                         throw new Exception();
-                    stream_filter_remove($s_filter); 
+                    stream_filter_remove($s_filter);
                     fclose($f);
                     if (!($f = fopen($temp, 'r')) || !($text = fread($f, filesize($temp))))
                         throw new Exception();
@@ -333,6 +333,7 @@ class MailFetcher {
                 $tolist['delivered-to'] = $delivered_to;
         }
 
+        $header['system_emails'] = array();
         $header['recipients'] = array();
         foreach($tolist as $source => $list) {
             foreach($list as $addr) {
@@ -344,8 +345,10 @@ class MailFetcher {
                             'source' => sprintf(_S("Email (%s)"),$source),
                             'name' => $this->mime_decode(@$addr->personal),
                             'email' => strtolower($addr->mailbox).'@'.$addr->host);
-                } elseif(!$header['emailId']) {
-                    $header['emailId'] = $emailId;
+                } elseif ($emailId) {
+                    $header['system_emails'][] = $emailId;
+                    if (!$header['emailId'])
+                        $header['emailId'] = $emailId;
                 }
             }
         }
@@ -361,14 +364,18 @@ class MailFetcher {
         }
 
         //BCCed?
-        if(!$header['emailId']) {
-            if ($headerinfo->bcc) {
-                foreach($headerinfo->bcc as $addr)
-                    if (($header['emailId'] = Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host)))
-                        break;
+        if ($bcc = $headerinfo->bcc) {
+            foreach ($bcc as $addr) {
+                if (!($emailId=Email::getIdByEmail($addr->mailbox.'@'.$addr->host)))
+                    continue;
+                $header['system_emails'][] = $emailId;
+                if (!$header['emailId'])
+                    $header['emailId'] = $emailId;
             }
         }
 
+        $header['system_emails'] = array_unique($header['system_emails']);
+
         // Ensure we have a message-id. If unable to read it out of the
         // email, use the hash of the entire email headers
         if (!$header['mid'] && $header['header']) {
@@ -685,6 +692,7 @@ class MailFetcher {
         $vars['emailId'] = $mailinfo['emailId'] ?: $this->getEmailId();
         $vars['to-email-id'] = $mailinfo['emailId'] ?: 0;
         $vars['mailflags'] = new ArrayObject();
+        $vars['system_emails'] = $mailinfo['system_emails'];
 
         if ($this->isBounceNotice($mid)) {
             // Fetch the original References and assign to 'references'
@@ -773,6 +781,7 @@ class MailFetcher {
 
         $seen = false;
         if (($entry = ThreadEntry::lookupByEmailHeaders($vars, $seen))
+            && ($entry instanceof ThreadEntry)
             && ($message = $entry->postEmail($vars))
         ) {
             if (!$message instanceof ThreadEntry)
@@ -781,6 +790,9 @@ class MailFetcher {
             // NOTE: This might not be a "ticket"
             $ticket = $message->getThread()->getObject();
         }
+        elseif ($entry && !$entry instanceof ThreadEntry) {
+          return null;
+        }
         elseif ($seen) {
             // Already processed, but for some reason (like rejection), no
             // thread item was created. Ignore the email
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 1564cc07735a2d7460d65eaff8d4935c15ecd668..a38027ebb724088a3705987a89a498c7bc96e822 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -647,6 +647,7 @@ class EmailDataParser {
         if (($dt = $parser->getDeliveredToAddressList()))
             $tolist['delivered-to'] = $dt;
 
+        $data['system_emails'] = array();
         foreach ($tolist as $source => $list) {
             foreach($list as $addr) {
                 if (!($emailId=Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host))) {
@@ -657,8 +658,10 @@ class EmailDataParser {
                         'source' => sprintf(_S("Email (%s)"), $source),
                         'name' => trim(@$addr->personal, '"'),
                         'email' => strtolower($addr->mailbox).'@'.$addr->host);
-                } elseif(!$data['emailId']) {
-                    $data['emailId'] = $emailId;
+                } elseif ($emailId) {
+                    $data['system_emails'][] = $emailId;
+                    if (!$data['emailId'])
+                        $data['emailId'] = $emailId;
                 }
             }
         }
@@ -680,16 +683,18 @@ class EmailDataParser {
 
 
         //maybe we got BCC'ed??
-        if(!$data['emailId']) {
-            $emailId =  0;
-            if($bcc = $parser->getBccAddressList()) {
-                foreach ($bcc as $addr)
-                    if(($emailId=Email::getIdByEmail($addr->mailbox.'@'.$addr->host)))
-                        break;
+        if($bcc = $parser->getBccAddressList()) {
+            foreach ($bcc as $addr) {
+                if (($emailId=Email::getIdByEmail($addr->mailbox.'@'.$addr->host))) {
+                    $data['system_emails'][] = $emailId;
+                    if (!$data['emailId'])
+                        $data['emailId'] =  $emailId;
+                }
             }
-            $data['emailId'] = $emailId;
         }
 
+        $data['system_emails'] = array_unique($data['system_emails']);
+
         if ($parser->isBounceNotice()) {
             // Fetch the original References and assign to 'references'
             if ($headers = $parser->getOriginalMessageHeaders()) {
diff --git a/include/class.thread.php b/include/class.thread.php
index 532a100f5e608c5c868d59a877e1be4762845d6e..99ce210cbba6dfa26ed44b377aa4b6402398440e 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -311,7 +311,7 @@ implements Searchable {
             break;
         case $to instanceof Dept:
             // Refered to the dept
-            if ($this->getReferral($to->getId,
+            if ($this->getReferral($to->getId(),
                         ObjectModel::OBJECT_TYPE_DEPT))
                 return true;
             break;
@@ -1314,6 +1314,11 @@ implements TemplateVariable {
                  )
          ) {
             $seen = true;
+            if ($mailinfo['system_emails']
+                    && ($t = $entry->getThread()->getObject())
+                    && $t instanceof Ticket)
+                $t->systemReferral($mailinfo['system_emails']);
+
             return $entry;
         }
 
diff --git a/include/class.ticket.php b/include/class.ticket.php
index c73b897fac858dbc4e719044cef0ce1639141503..d4909a118d3c573b9f51f8d6dd3308c964349e30 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2376,7 +2376,7 @@ implements RestrictedAccess, Threadable, Searchable {
             }
             break;
         case $referee instanceof Dept:
-            if ($this->getTeamId() == $referee->getId()) {
+            if ($this->getDeptId() == $referee->getId()) {
                 $errors['dept'] = sprintf(__('%s is already in %s'),
                         __('Ticket'),
                         __('the department')
@@ -2401,6 +2401,24 @@ implements RestrictedAccess, Threadable, Searchable {
         return true;
     }
 
+    function systemReferral($emails) {
+
+        if (!$this->thread)
+            return;
+
+        foreach ($emails as $id) {
+            if ($id != $this->email_id
+                    && ($email=Email::lookup($id))
+                    && $this->getDeptId() != $email->getDeptId()
+                    && ($dept=Dept::lookup($email->getDeptId()))
+                    && $this->thread->refer($dept)
+                    )
+                $this->logEvent('referred',
+                            array('dept' => $dept->getId()));
+        }
+
+    }
+
     //Change ownership
     function changeOwner($user) {
         global $thisstaff;
@@ -3746,6 +3764,10 @@ implements RestrictedAccess, Threadable, Searchable {
                 $autorespond = false;
         }
 
+
+        if ($vars['system_emails'])
+            $ticket->systemReferral($vars['system_emails']);
+
         // Check department's auto response settings
         // XXX: Dept. setting doesn't affect canned responses.
         if ($autorespond && $dept && !$dept->autoRespONNewTicket())