diff --git a/css_emimage.module b/css_emimage.module
old mode 100755
new mode 100644
index 626b2ef..cf8f1e1
--- a/css_emimage.module
+++ b/css_emimage.module
@@ -58,13 +58,314 @@ function css_emimage_form_alter(&$form, &$form_state, $form_id) {
 }
 
 /**
+ * Implementation of hook_advagg_filenames_alter().
+ *
+ * See if any CSS file in the desired bundle contains a local image in it.
+ */
+function css_emimage_advagg_filenames_alter(&$filenames) {
+  global $base_path;
+  $output = array();
+  foreach ($filenames as $values) {
+    // Set values.
+    $filetype = $values['filetype'];
+    $files = $values['files'];
+    $counter = $values['counter'];
+    $bundle_md5 = $values['bundle_md5'];
+
+
+    // Only operate on CSS files.
+    if ($filetype != 'css') {
+      $output[] = $values;
+      continue;
+    }
+
+    $cached_data_key = 'css_emimage_' . $bundle_md5;
+    // Try cache first; cache table is cache_advagg_bundle_reuse.
+    $cached_data = advagg_cached_bundle_get($cached_data_key, 'css_emimage_filenames_alter');
+    if (!empty($cached_data)) {
+      // verify cache_css_emimage_advagg has the data we need.
+      $good = TRUE;
+      foreach ($cached_data as $info) {
+        $saved = cache_get($info['bundle_md5'], 'cache_css_emimage_advagg');
+        if (empty($saved->data)) {
+          $good = FALSE;
+        }
+      }
+      if ($good) {
+        $output = array_merge($output, $cached_data);
+        continue;
+      }
+    }
+    $cached_data = array();
+
+    // Load up each CSS file.
+    $has_image = FALSE;
+    foreach ($files as $filename) {
+      $filename_md5 = md5($filename);
+      $data = advagg_get_file_data($filename_md5);
+      if (!empty($data['css_emimage'])) {
+        $has_image = TRUE;
+        break;
+      }
+
+      if (!isset($data['css_emimage'])) {
+        // Search css file to see if it contains an image.
+        if (!advagg_file_exists($filename)) {
+          continue;
+        }
+        $css = file_get_contents($filename);
+
+        // See if CSS file contains an image.
+        $pattern = '/(background(?:-image)?|list-style(?:-image)?):[^{};)]*?((?:none|url\([\'"]?(.+?)[\'"]?\)))([^{};]*)/i';
+        if (preg_match_all($pattern, $css, $matches) > 0) {
+          $images = $matches[3];
+          foreach ($matches[3] as $imagename) {
+            if (empty($imagename)) {
+              continue;
+            }
+
+            // Strip base path from filename.
+            $imagename = preg_replace('/^' . preg_quote($base_path, '/') .'/i', '', $imagename);
+            // If not a local file, continue the search.
+            if (file_exists($filename) == FALSE) {
+              continue;
+            }
+
+            // Break on the first local image we hit.
+            $has_image = TRUE;
+            break;
+          }
+        }
+
+        if (!$has_image) {
+          $data['css_emimage'] = 0;
+          advagg_set_file_data($filename_md5, $data);
+        }
+      }
+
+      // Jump out of loop if we found a local image.
+      if ($has_image) {
+        break;
+      }
+    }
+
+    if ($has_image) {
+      // See if this has already been "processed".
+      $saved = cache_get($bundle_md5, 'cache_css_emimage_advagg');
+      if (!empty($saved->data)) {
+        if ($saved->data != 'none') {
+          $output[] = $values;
+          $cached_data = array($values);
+          cache_set($cached_data_key, $cached_data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
+          continue;
+        }
+        else {
+          // css file used to not have any images, now it does.
+          cache_clear_all($bundle_md5, 'cache_advagg');
+        }
+      }
+
+      // Insert base css md5 into cache_css_emimage_advagg.
+      $values['bundle_md5'] = md5($bundle_md5 . 'base');
+      $saved = cache_get($values['bundle_md5'], 'cache_css_emimage_advagg');
+      if (empty($saved->data)) {
+        cache_set($values['bundle_md5'], 'base', 'cache_css_emimage_advagg');
+      }
+      $output[] = $values;
+      $cached_data[] = $values;
+
+      // Insert emimage css md5 into cache_css_emimage_advagg.
+      $values['bundle_md5'] = md5($bundle_md5 . 'emimage');
+      $saved = cache_get($values['bundle_md5'], 'cache_css_emimage_advagg');
+      if (empty($saved->data)) {
+        cache_set($values['bundle_md5'], 'emimage', 'cache_css_emimage_advagg');
+      }
+      $output[] = $values;
+      $cached_data[] = $values;
+
+      // Insert orig css md5 into cache_css_emimage_advagg.
+      $values['bundle_md5'] = md5($bundle_md5 . 'orig');
+      $saved = cache_get($values['bundle_md5'], 'cache_css_emimage_advagg');
+      if (empty($saved->data)) {
+        cache_set($values['bundle_md5'], 'orig', 'cache_css_emimage_advagg');
+      }
+      $output[] = $values;
+      $cached_data[] = $values;
+      cache_set($cached_data_key, $cached_data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
+    }
+    else {
+      // Insert none css md5 into cache_css_emimage_advagg; no processing needed.
+      $saved = cache_get($values['bundle_md5'], 'cache_css_emimage_advagg');
+      if (empty($saved->data)) {
+        cache_set($values['bundle_md5'], 'none', 'cache_css_emimage_advagg');
+      }
+      $output[] = $values;
+      $data = array($values);
+      cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
+
+      // Remove bundle_md5s if file use to have an image and now does not.
+      $key = md5($bundle_md5 . 'base');
+      $saved = cache_get($key, 'cache_css_emimage_advagg');
+      if (!empty($saved->data)) {
+        cache_clear_all($key, 'cache_advagg');
+      }
+      $key = md5($bundle_md5 . 'emimage');
+      $saved = cache_get($key, 'cache_css_emimage_advagg');
+      if (!empty($saved->data)) {
+        cache_clear_all($key, 'cache_advagg');
+      }
+      $key = md5($bundle_md5 . 'orig');
+      $saved = cache_get($key, 'cache_css_emimage_advagg');
+      if (!empty($saved->data)) {
+        cache_clear_all($key, 'cache_advagg');
+      }
+    }
+  }
+
+  $filenames = $output;
+}
+
+/**
+ * Implementation of hook_advagg_css_extra_alter().
+ *
+ * Set the CSS prefix and suffix.
+ */
+function css_emimage_advagg_css_extra_alter(&$values) {
+  list($filename, $bundle_md5, $prefix, $suffix) = $values;
+  $type = cache_get($bundle_md5, 'cache_css_emimage_advagg');
+  if (!empty($type->data)) {
+    $type = $type->data;
+  }
+
+  if ($type == 'base') {
+    $prefix = "<!--[if gte IE 8]><!-->";
+    $suffix = "<!--<![endif]-->";
+  }
+  elseif ($type == 'emimage') {
+    $prefix = "<!--[if gte IE 8]><!-->";
+    $suffix = "<!--<![endif]-->";
+  }
+  elseif ($type == 'orig') {
+    $prefix = "<!--[if lt IE 8]>";
+    $suffix = "<![endif]-->";
+  }
+  $values = array($filename, $bundle_md5, $prefix, $suffix);
+}
+
+/**
+ * Implementation of hook_advagg_css_alter().
+ *
+ * Given CSS data embed images into it.
+ */
+function css_emimage_advagg_css_alter($data, $files, $bundle_md5) {
+  $type = cache_get($bundle_md5, 'cache_css_emimage_advagg');
+  if (!empty($type->data)) {
+    $type = $type->data;
+  }
+
+  if (empty($type)) {
+    return;
+  }
+  if ($type == 'none' || $type == 'orig') {
+    return;
+  }
+
+  _css_emimage_text_processor($data, $bundle_md5, $type);
+
+  // Make sure data sent back is not empty.
+  // Workaround needed for advagg async mode.
+  if (empty($data)) {
+    $data = 'html {display:block;}';
+  }
+}
+
+/**
+ * Implementation of hook_advagg_files_table().
+ *
+ * See if any images referenced in a CSS file has changed.
+ */
+function css_emimage_advagg_files_table($row, $checksum) {
+  global $base_path;
+
+  // Only operate on CSS files.
+  if ($row['filetype'] != 'css') {
+    return;
+  }
+
+  // Load the CSS file.
+  $images = array();
+  $css = advagg_build_css_bundle(array($row['filename']));
+  $data = unserialize($row['data']);
+
+  // See if CSS file contains an image.
+  $pattern = '/(background(?:-image)?|list-style(?:-image)?):[^{};)]*?((?:none|url\([\'"]?(.+?)[\'"]?\)))([^{};]*)/i';
+  if (preg_match_all($pattern, $css, $matches) > 0) {
+    $images = $matches[3];
+  }
+
+  // In no images bail out.
+  if (empty($images)) {
+    if (isset($data['css_emimage']) && $data['css_emimage'] == 0) {
+      return;
+    }
+    $data['css_emimage'] = 0;
+    advagg_set_file_data($row['filename_md5'], $data);
+    return;
+  }
+
+
+  $out = array();
+  $rebuild = FALSE;
+  $save = FALSE;
+  foreach ($images as $filename) {
+    if (empty($filename)) {
+      continue;
+    }
+    // Strip base path from filename.
+    $filename = preg_replace('/^' . preg_quote($base_path, '/') .'/i', '', $filename);
+    if (file_exists($filename) == FALSE) {
+      continue;
+    }
+
+    // Get the checksum of each image.
+    $checksum = advagg_checksum($filename);
+    if (!empty($data['css_emimage'][$filename])) {
+      if ($data['css_emimage'][$filename] != $checksum) {
+        $rebuild = TRUE;
+        $save = TRUE;
+      }
+    }
+    else {
+      $save = TRUE;
+    }
+
+    if (!is_array($data['css_emimage'])) {
+      unset($data['css_emimage']);
+    }
+    $data['css_emimage'][$filename] = $checksum;
+  }
+
+  if ($save) {
+    advagg_set_file_data($row['filename_md5'], $data);
+  }
+  return $rebuild;
+}
+
+/**
+ * Implementation of hook_advagg_master_reset().
+ */
+function css_emimage_advagg_master_reset() {
+  cache_clear_all('*', 'cache_css_emimage_advagg', TRUE);
+}
+
+/**
  * Implementation of hook_theme_registry_alter().
  *
  * Make css_emimage's page preprocess function run after everything else.
  * If the css_gzip module is installed, move it's preprocess function after ours.
  */
 function css_emimage_theme_registry_alter(&$theme_registry) {
-  if (isset($theme_registry['page'])) {
+  if (isset($theme_registry['page']) && !module_exists('advagg')) {
     // Move our preprocess function after everything else.
     if (($key = array_search('css_emimage_preprocess_page', $theme_registry['page']['preprocess functions'])) !== FALSE) {
       unset($theme_registry['page']['preprocess functions'][$key]);
@@ -107,65 +408,20 @@ function _css_emimage_process($styles) {
 
       // Save the processed CSS file if it doesn't exist yet.
       if (!file_exists($emimage_file_path) || filemtime($aggregated_file_path) > filemtime($emimage_file_path)) {
-        _css_emimage_collect_static(array(array(), array())); // Reset the processed declarations.
         $contents = $orig_contents = file_get_contents($aggregated_file_path);
-        $datauri_css = '';
-
-        $pattern = '/([^{}]+){([^{}]*?(background(?:-image)?|list-style(?:-image)?):[^{};)]*?(?:none|url\([\'"]?.+?[\'"]?\))[^{}]*)}/i';
-        $contents = preg_replace_callback($pattern, '_css_emimage_replace', $contents);
-
-        if (!is_null($contents)) {
-          list($declarations, $file_stats) = _css_emimage_collect_static();
-
-          // Check for duplicate images and exclude those exceeding our duplication limit.
-          // Sum the amount of data being embedded.
-          $datauri_total_length = 0;
-          foreach ($file_stats as $fs) {
-            if (count($fs['indices']) > 1 && $fs['total_length'] > variable_get('css_emimage_duplicate_embed_limit', CSS_EMIMAGE_DUPLICATE_EMBED_LIMIT)) {
-              foreach ($fs['indices'] as $fsi) {
-                $declarations[$fsi]['base64'] = '';
-              }
-            }
-            else {
-              $datauri_total_length += $fs['total_length'];
-            }
-          }
 
-          list($ext_contents, $ext_data) = _css_emimage_build_external($contents, $declarations);
+        // Save a copy of the original aggregated CSS for IE < 8 fallback.
+        file_save_data($orig_contents, $orig_file_path, FILE_EXISTS_REPLACE);
 
-          // If the amount of data being embedded is within the inline limit, inline the data URIs;
-          // otherwise, store the data URIs in a separate CSS file.
-          if (variable_get('css_emimage_force_inline', 0) || ($datauri_total_length && $datauri_total_length <= variable_get('css_emimage_inline_datauri_limit', CSS_EMIMAGE_INLINE_DATAURI_LIMIT))) {
-            $inline = _css_emimage_build_inline($contents, $declarations);
-            if (strlen($inline) < (strlen($ext_contents) + strlen($ext_data))) {
-              $datauri_css = $inline;
-            }
-            else {
-              $datauri_css = "$ext_contents\n$ext_data";
-            }
-            $contents = '';
-          }
-          else {
-            $contents = $ext_contents;
-            $datauri_css = $ext_data;
-          }
-
-          // Save the modified aggregated CSS file.
-          file_save_data($contents, $aggregated_file_path, FILE_EXISTS_REPLACE);
-          // Save a copy of the original aggregated CSS for IE < 8 fallback.
-          file_save_data($orig_contents, $orig_file_path, FILE_EXISTS_REPLACE);
-        }
-        else {
-          $error_code = preg_last_error();
-          $error_messages = array(PREG_NO_ERROR => 'NO_ERROR', PREG_INTERNAL_ERROR => 'INTERNAL_ERROR', PREG_BACKTRACK_LIMIT_ERROR => 'BACKTRACK_LIMIT_ERROR', PREG_RECURSION_LIMIT_ERROR => 'RECURSION_LIMIT_ERROR', PREG_BAD_UTF8_ERROR => 'BAD_UTF8_ERROR', PREG_BAD_UTF8_OFFSET_ERROR => 'BAD_UTF8_OFFSET_ERROR');
-          watchdog('css_emimage', 'Error while trying to embed images in your CSS, falling back to unmodified CSS. PCRE error was: !error.',
-            array('!error' => array_key_exists($error_code, $error_messages) ? $error_messages[$error_code] : $error_code), WATCHDOG_ERROR);
-        }
+        // Save the modified aggregated CSS file.
+        _css_emimage_text_processor($orig_contents, $orig_file_path, 'base');
+        file_save_data($orig_contents, $aggregated_file_path, FILE_EXISTS_REPLACE);
 
         // Save the CSS file containing the embedded images.
         // This may be empty, but we use the file as a flag to prevent
         // processing the CSS on every uncached request.
-        file_save_data($datauri_css, $emimage_file_path, FILE_EXISTS_REPLACE);
+        _css_emimage_text_processor($contents, $orig_file_path, 'emimage');
+        file_save_data($contents, $emimage_file_path, FILE_EXISTS_REPLACE);
       }
 
       // Replace the aggregated file with the processed CSS file.
@@ -182,6 +438,86 @@ function _css_emimage_process($styles) {
 }
 
 /**
+ * Process the css text and replace it with image data where necessary.
+ *
+ * @param $data
+ *    CSS text data to process over.
+ * @param $key
+ *    Used for a static cache.
+ * @param $type
+ *    The type of css wanted back, base or emimage.
+ */
+function _css_emimage_text_processor(&$data, $key, $type) {
+  static $values = array();
+
+  // This only processes base & emimage types.
+  if ($type != 'base' || $type != 'emimage') {
+    return;
+  }
+
+  if (empty($values[$key][$type])) {
+    // Do magic; code could be improved.
+    _css_emimage_collect_static(array(array(), array())); // Reset the processed declarations.
+    $contents = $data;
+    $datauri_css = '';
+
+    $pattern = '/([^{}]+){([^{}]*?(background(?:-image)?|list-style(?:-image)?):[^{};)]*?(?:none|url\([\'"]?.+?[\'"]?\))[^{}]*)}/i';
+    $contents = preg_replace_callback($pattern, '_css_emimage_replace', $contents);
+
+    if (!is_null($contents)) {
+      list($declarations, $file_stats) = _css_emimage_collect_static();
+
+      // Check for duplicate images and exclude those exceeding our duplication limit.
+      // Sum the amount of data being embedded.
+      $datauri_total_length = 0;
+      foreach ($file_stats as $fs) {
+        if (count($fs['indices']) > 1 && $fs['total_length'] > variable_get('css_emimage_duplicate_embed_limit', CSS_EMIMAGE_DUPLICATE_EMBED_LIMIT)) {
+          foreach ($fs['indices'] as $fsi) {
+            $declarations[$fsi]['base64'] = '';
+          }
+        }
+        else {
+          $datauri_total_length += $fs['total_length'];
+        }
+      }
+
+      list($ext_contents, $ext_data) = _css_emimage_build_external($contents, $declarations);
+
+      // If the amount of data being embedded is within the inline limit, inline the data URIs;
+      // otherwise, store the data URIs in a separate CSS file.
+      if (variable_get('css_emimage_force_inline', 0) || ($datauri_total_length && $datauri_total_length <= variable_get('css_emimage_inline_datauri_limit', CSS_EMIMAGE_INLINE_DATAURI_LIMIT))) {
+        $inline = _css_emimage_build_inline($contents, $declarations);
+        if (strlen($inline) < (strlen($ext_contents) + strlen($ext_data))) {
+          $datauri_css = $inline;
+        }
+        else {
+          $datauri_css = "$ext_contents\n$ext_data";
+        }
+        $contents = '';
+      }
+      else {
+        $contents = $ext_contents;
+        $datauri_css = $ext_data;
+      }
+    }
+    else {
+      $error_code = preg_last_error();
+      $error_messages = array(PREG_NO_ERROR => 'NO_ERROR', PREG_INTERNAL_ERROR => 'INTERNAL_ERROR', PREG_BACKTRACK_LIMIT_ERROR => 'BACKTRACK_LIMIT_ERROR', PREG_RECURSION_LIMIT_ERROR => 'RECURSION_LIMIT_ERROR', PREG_BAD_UTF8_ERROR => 'BAD_UTF8_ERROR', PREG_BAD_UTF8_OFFSET_ERROR => 'BAD_UTF8_OFFSET_ERROR');
+      watchdog('css_emimage', 'Error while trying to embed images in your CSS, falling back to unmodified CSS. PCRE error was: !error.',
+        array('!error' => array_key_exists($error_code, $error_messages) ? $error_messages[$error_code] : $error_code), WATCHDOG_ERROR);
+      return;
+    }
+    $values[$key]['base'] = $contents;
+    $values[$key]['emimage'] = $datauri_css;
+  }
+
+  // Send data back.
+  $data = $values[$key][$type];
+  // Free memory, values only gets used once.
+  unset($values[$key][$type]);
+}
+
+/**
  * preg_replace_callback() callback to replace URLs with data URIs.
  */
 function _css_emimage_replace($matches) {
