diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 378c68c1959e2a5c6ff4d442a9b8282c43c7aee3..91fa3f498e5bb4eaeae06a824efdb1f3981b1ee5 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -32,9 +32,9 @@ class Mail_Parse {
 
     var $tnef = false;      // TNEF encoded mail
 
-    function Mail_parse($mimeMessage, $charset=null){
+    function Mail_parse(&$mimeMessage, $charset=null){
 
-        $this->mime_message = $mimeMessage;
+        $this->mime_message = &$mimeMessage;
 
         if($charset)
             $this->charset = $charset;
@@ -54,18 +54,18 @@ class Mail_Parse {
 
         $params = array('crlf'          => "\r\n",
                         'charset'       => $this->charset,
-                        'input'         => $this->mime_message,
                         'include_bodies'=> $this->include_bodies,
                         'decode_headers'=> $this->decode_headers,
                         'decode_bodies' => $this->decode_bodies);
 
-        $this->struct=Mail_mimeDecode::decode($params);
+        $this->splitBodyHeader();
+
+        $decoder = new Mail_mimeDecode($this->mime_message);
+        $this->struct = $decoder->decode($params);
 
         if (PEAR::isError($this->struct))
             return false;
 
-        $this->splitBodyHeader();
-
         // Handle wrapped emails when forwarded
         if ($this->struct && $this->struct->parts) {
             $outer = $this->struct;
@@ -119,7 +119,7 @@ class Mail_Parse {
 
     function splitBodyHeader() {
         $match = array();
-        if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s",
+        if (preg_match("/^(.*?)\r?\n\r?\n./s",
                 $this->mime_message,
                 $match)) {
             $this->header=$match[1];
diff --git a/include/pear/Mail/mimeDecode.php b/include/pear/Mail/mimeDecode.php
index b300195824c30528f7ff44987679fd11875889e3..1f7d412d4b44b8c4fadff9497c61e159af4a7cd3 100644
--- a/include/pear/Mail/mimeDecode.php
+++ b/include/pear/Mail/mimeDecode.php
@@ -91,14 +91,6 @@ require_once 'PEAR.php';
  */
 class Mail_mimeDecode extends PEAR
 {
-    /**
-     * The raw email to decode
-     *
-     * @var    string
-     * @access private
-     */
-    var $_input;
-
     /**
      * The header part of the input
      *
@@ -157,13 +149,13 @@ class Mail_mimeDecode extends PEAR
      * @param string The input to decode
      * @access public
      */
-    function Mail_mimeDecode($input)
+    function Mail_mimeDecode(&$input)
     {
         list($header, $body)   = $this->_splitBodyHeader($input);
 
-        $this->_input          = $input;
-        $this->_header         = $header;
-        $this->_body           = $body;
+        $this->_input          = &$input;
+        $this->_header         = &$header;
+        $this->_body           = &$body;
         $this->_decode_bodies  = false;
         $this->_include_bodies = true;
     }
@@ -241,7 +233,7 @@ class Mail_mimeDecode extends PEAR
      * @return object Results of decoding process
      * @access private
      */
-    function _decode($headers, $body, $default_ctype = 'text/plain')
+    function _decode(&$headers, &$body, $default_ctype = 'text/plain')
     {
         $return = new stdClass;
         $return->headers = array();
@@ -324,8 +316,9 @@ class Mail_mimeDecode extends PEAR
                     $default_ctype = (strtolower($content_type['value']) === 'multipart/digest') ? 'message/rfc822' : 'text/plain';
 
                     $parts = $this->_boundarySplit($body, $content_type['other']['boundary']);
-                    for ($i = 0; $i < count($parts); $i++) {
-                        list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
+                    while (count($parts)) {
+                        $part = array_shift($parts);
+                        list($part_header, $part_body) = $this->_splitBodyHeader($part);
                         $part = $this->_decode($part_header, $part_body, $default_ctype);
                         if($part === false)
                             $part = $this->raiseError($this->_error);
@@ -409,10 +402,13 @@ class Mail_mimeDecode extends PEAR
      * @return array Contains header and body section
      * @access private
      */
-    function _splitBodyHeader($input)
+    function _splitBodyHeader(&$input)
     {
-        if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) {
-            return array($match[1], $match[2]);
+        // The headers should end within the first 500k of the email...
+        $to_check = substr($input, 0, 500<<10);
+        if (preg_match("/^.*?(\r?\n\r?\n)(.)/s", $to_check, $match, PREG_OFFSET_CAPTURE)) {
+            return array(substr($input, 0, $match[1][1]),
+                   substr($input, $match[2][1]));
         }
         $this->_error = 'Could not split header and body';
         return false;