diff --git a/sites/all/modules/css_emimage/css_emimage.module b/sites/all/modules/css_emimage/css_emimage.module
index 8f7d383..d3686d1 100644
--- a/sites/all/modules/css_emimage/css_emimage.module
+++ b/sites/all/modules/css_emimage/css_emimage.module
@@ -75,11 +75,11 @@ function _css_emimage_process($styles) {
       // Save the processed CSS file if it doesn't exist yet.
       if (!file_exists($datauri_file_path)) {
         $contents = file_get_contents(file_directory_path() . $aggregated_file_name);
-        $contents = preg_replace_callback('/(?:^|})'. //Starts with the end of previous css instruction, or beginning of file.
-        								  '([^}\\/]+' . //Starts with a css identifier : no forward slash should be found here 
+        $contents = preg_replace_callback('/(?<=^|})'. //Starts with the end of previous css instruction, or beginning of file.
+        								  '[^}\\/]+' . //Starts with a css identifier : no forward slash should be found here 
         								   '{[^{}\\/]+' . //Followed by a { then anything really, not closed of course. We exclude / which shouldn't be found anywhere except in an eventual data block.
         								    '(url\([\'"]?'. preg_quote(base_path(), '/') .'([^\'")]+\.(?:gif|png|jpg))[\'"]?\))' . //Followed by a url to an image
-        								  '[^{}]*})/i', //Followed by anything again
+        								  '[^{}]*}/i', //Followed by anything again
         									 '_css_emimage_replace', $contents);
         
         if (is_null($contents)) // There was an error with the regular expression
@@ -109,13 +109,14 @@ function _css_emimage_process($styles) {
  */
 function _css_emimage_replace($matches) {
   $replacement = $matches[0];
-  if ($image = image_get_info($matches[3])) {
+  
+  if ($image = image_get_info($matches[2])) {
     $ielimit = variable_get('css_emimage_ielimit', 1); // only embed images less than 32KB, thanks IE
     if (!$ielimit || ($ielimit && ($image['file_size']*1.3333) < 32768)) {
-      $replacement = str_replace($matches[2], 'url(data:'. $image['mime_type'] .';base64,'. base64_encode(file_get_contents($matches[3])). ')', $replacement);
+      $replacement = str_replace($matches[1], 'url(data:'. $image['mime_type'] .';base64,'. base64_encode(file_get_contents($matches[2])). ')', $replacement);
       
       // Overrides for IE6 and IE7
-      $replacement .= "\n * html ". $matches[1] ."\n *+html ". $matches[1] ."\n";
+      $replacement .= "\n * html ". $matches[0] ."\n *+html ". $matches[0] ."\n";
     }
   }
   return $replacement;
