diff --git a/advagg.admin.inc b/advagg.admin.inc
index 63ec56c..a971e04 100644
--- a/advagg.admin.inc
+++ b/advagg.admin.inc
@@ -14,27 +14,41 @@
 function advagg_admin_settings_form($form, $form_state) {
 
   // Simple checkbox settings.
-  $form['advagg_enabled'] = array(
+  $form['global'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Global Options'),
+  );
+  $form['global']['advagg_enabled'] = array(
     '#type' => 'checkbox',
     '#title' => t('Enable advanced aggregation'),
     '#default_value' => variable_get('advagg_enabled', ADVAGG_ENABLED),
   );
-  $form['advagg_gzip'] = array(
+  $form['global']['advagg_gzip'] = array(
     '#type' => 'checkbox',
     '#title' => t('Create .gz files'),
     '#default_value' => variable_get('advagg_gzip', ADVAGG_GZIP),
   );
-  $form['advagg_core_groups'] = array(
+  $form['global']['advagg_core_groups'] = array(
     '#type' => 'checkbox',
     '#title' => t('Use cores grouping logic'),
     '#default_value' => variable_get('advagg_core_groups', ADVAGG_CORE_GROUPS),
   );
-  $form['advagg_use_httprl'] = array(
+  $form['global']['advagg_use_httprl'] = array(
     '#type' => 'checkbox',
     '#title' => t('Use HTTPRL to generate aggregates.'),
     '#default_value' => module_exists('httprl') ? variable_get('advagg_use_httprl', ADVAGG_USE_HTTPRL) : FALSE,
     '#disabled' => module_exists('httprl') ? FALSE : TRUE,
   );
+  $form['css'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('CSS Options'),
+  );
+  $form['css']['advagg_combine_css_media'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Combine css files by using media queries'),
+    '#default_value' => variable_get('advagg_combine_css_media', ADVAGG_COMBINE_CSS_MEDIA),
+    '#description' => t('Use cores grouping logic needs to be unchecked in order for this to work.'),
+  );
 
   // Clear the cache bins on submit.
   $form['#submit'][] = 'advagg_admin_settings_form_submit';
diff --git a/advagg.advagg.inc b/advagg.advagg.inc
index 95ca3f5..d4b2522 100644
--- a/advagg.advagg.inc
+++ b/advagg.advagg.inc
@@ -52,7 +52,7 @@ function advagg_advagg_save_aggregate_alter(&$files_to_save, $aggregate_settings
 }
 
 /**
- * Implements hook_advagg_advagg_build_aggregate_plans_alter().
+ * Implements hook_advagg_build_aggregate_plans_alter().
  *
  * Used to alter the plan so it has the same grouping as cores.
  *
@@ -74,12 +74,7 @@ function advagg_advagg_build_aggregate_plans_alter(&$files, &$modified, $type) {
   foreach ($files as $filename => $data) {
     $group = NULL;
     $every_page = NULL;
-    foreach ($data as $key => $fileinfo) {
-      // Skip non numeric keys.
-      if (!is_numeric($key)) {
-        continue;
-      }
-
+    foreach ($data['files'] as $fileinfo) {
       // Grouped by group & every_page variables.
       if (is_null($group)) {
         $group = $fileinfo['group'];
diff --git a/advagg.inc b/advagg.inc
index fcc2185..66c1030 100644
--- a/advagg.inc
+++ b/advagg.inc
@@ -123,11 +123,18 @@ function advagg_insert_aggregate($files, $aggregate_filenames_hash) {
       continue;
     }
 
+    // Store settings for this file that depend on how it was added.
+    $settings = array();
+    if (isset($file_meta_data['media_query'])) {
+      $settings['media'] = $file_meta_data['media_query'];
+    }
+
     // Write record into the database.
     $record = array(
       'aggregate_filenames_hash' => $aggregate_filenames_hash,
       'filename_hash' => $file_meta_data['filename_hash'],
       'porder' => $count,
+      'settings' => $settings,
     );
     if (drupal_write_record('advagg_aggregates', $record)) {
       $write_done = TRUE;
@@ -233,15 +240,10 @@ function advagg_update_all_atimes($files) {
 function advagg_generate_filenames($groups, $type) {
   $files = array();
   foreach ($groups as $key => $data) {
-    foreach ($data as $values) {
-      // Build a list of filenames.
-      $group_of_filenames = array();
-      foreach ($values as $file_info) {
-        $group_of_filenames[] = $file_info['data'];
-      }
+    foreach ($data as $files_with_meta_data) {
 
       // Get the aggregate filename & info about each file.
-      $aggregate_info = advagg_get_aggregate_info_from_files($type, $group_of_filenames);
+      $aggregate_info = advagg_get_aggregate_info_from_files($type, $files_with_meta_data);
       $values['files'] = $aggregate_info[1];
       $values['aggregate_filenames_hash'] = $aggregate_info[2];
       $values['aggregate_contents_hash'] = $aggregate_info[3];
@@ -263,12 +265,13 @@ function advagg_generate_filenames($groups, $type) {
  * @return
  *   array
  */
-function advagg_get_aggregate_info_from_files($type, $files) {
+function advagg_get_aggregate_info_from_files($type, $files_with_meta_data) {
   $filename_hashes = array();
   $content_hashes = array();
   $filenames = array();
-  foreach ($files as $filename) {
-    $info = advagg_get_info_on_file($filename);
+  foreach ($files_with_meta_data as $info) {
+    $filename = $info['data'];
+    $info += advagg_get_info_on_file($filename);
     // Skip if file doesn't exist.
     if (empty($info['content_hash'])) {
       continue;
@@ -413,6 +416,7 @@ function advagg_generate_groups($files_to_aggregate) {
   $groups = array();
   $count = 0;
   $location = 0;
+
   $media = '';
   $defer = '';
   $cache = '';
@@ -426,9 +430,15 @@ function advagg_generate_groups($files_to_aggregate) {
         // Check to see if media, browsers, defer, cache, or scope has changed
         // from the previous run of this loop.
         $changed = FALSE;
-        if (isset($file_info['media']) && $media != $file_info['media']) {
-          $changed = TRUE;
-          $media = $file_info['media'];
+        if (isset($file_info['media'])) {
+          if (variable_get('advagg_combine_css_media', ADVAGG_COMBINE_CSS_MEDIA)) {
+            $file_info['media_query'] = $file_info['media'];
+            $file_info['media'] = 'all';
+          }
+          elseif ($media != $file_info['media']) {
+            $changed = TRUE;
+            $media = $file_info['media'];
+          }
         }
         if (isset($file_info['browsers'])) {
           $diff = array_merge(array_diff($file_info['browsers'], $browsers), array_diff($browsers, $file_info['browsers']));
@@ -505,7 +515,7 @@ function advagg_build_aggregate_plans($files_to_aggregate, $type) {
   // Build the plan.
   $plans = array();
   foreach ($files as $agg_filename => $values) {
-    $first = reset($values);
+    $first = reset($values['files']);
     $url = ($type == 'css') ? $css_path[0] : $js_path[0];
     $path = ($type == 'css') ? $css_path[1] : $js_path[1];
     $plans[] = array(
diff --git a/advagg.install b/advagg.install
index 57d798f..7f59828 100644
--- a/advagg.install
+++ b/advagg.install
@@ -145,6 +145,14 @@ function advagg_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'settings' => array(
+        'description' => 'Extra data about this file and how it is used in this aggregate.',
+        'type' => 'blob',
+        'not null' => TRUE,
+        'size' => 'big',
+        'translatable' => TRUE,
+        'serialize' => TRUE,
+      ),
     ),
     'indexes' => array(
       'porder' => array('porder'),
diff --git a/advagg.missing.inc b/advagg.missing.inc
index 74f21dc..82152e0 100644
--- a/advagg.missing.inc
+++ b/advagg.missing.inc
@@ -304,13 +304,14 @@ function advagg_get_files_from_hashes($type, $aggregate_filenames_hash, $aggrega
   $query->join($subquery_aggregates, 'aa', 'aa.aggregate_filenames_hash=aav.aggregate_filenames_hash');
   $query->join($subquery_files, 'af', 'af.filename_hash=aa.filename_hash');
   $query = $query->fields('af', array('filename'))
+    ->fields('aa', array('settings'))
     ->orderBy('porder', 'ASC')
     ->execute();
 
   // Add in files that are included in this aggregate.
   $files = array();
-  while ($file = $query->fetchField()) {
-    $files[] = $file;
+  foreach ($query as $value) {
+    $files[$value->filename] = unserialize($value->settings);
   }
   return $files;
 }
@@ -332,7 +333,24 @@ function advagg_get_css_aggregate_contents($files, $aggregate_settings) {
 
   $data = '';
   if (!empty($files)) {
-    foreach ($files as $file) {
+    $media_changes = FALSE;
+    $last_media = NULL;
+    foreach ($files as $file => $settings) {
+      if (!isset($settings['media'])) {
+        continue;
+      }
+      if (is_null($last_media)) {
+        $last_media = $settings['media'];
+        continue;
+      }
+      if ($settings['media'] != $last_media) {
+        $media_changes = TRUE;
+        break;
+      }
+    }
+    $last_media = NULL;
+    $import_statements = array();
+    foreach ($files as $file => $settings) {
       $contents = drupal_load_stylesheet($file, $optimize);
 
       // Build the base URL of this CSS file: start with the full URL.
@@ -352,15 +370,47 @@ function advagg_get_css_aggregate_contents($files, $aggregate_settings) {
       // Allow other modules to modify this files contents.
       // Call hook_advagg_get_css_file_contents_alter().
       drupal_alter('advagg_get_css_file_contents', $contents, $file, $aggregate_settings);
+
+      if ($media_changes) {
+        // Start of stylesheet.
+        if (is_null($last_media)) {
+          $last_media = $settings['media'];
+          $contents = '@media ' . $last_media . ' {' . $contents;
+        }
+        // Media changes.
+        if ($settings['media'] != $last_media) {
+          $last_media = $settings['media'];
+          $contents = '} @media ' . $last_media . ' {' . $contents;
+        }
+      }
+      // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
+      // @import rules must proceed any other style, so we move those to the top.
+      $regexp = '/@import[^;]+;/i';
+      preg_match_all($regexp, $contents, $matches);
+      $contents = preg_replace($regexp, '', $contents);
+      $import_statements[] = array($last_media, $matches[0]);
+
       $data .= $contents;
     }
 
-    // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
-    // @import rules must proceed any other style, so we move those to the top.
-    $regexp = '/@import[^;]+;/i';
-    preg_match_all($regexp, $data, $matches);
-    $data = preg_replace($regexp, '', $data);
-    $data = implode('', $matches[0]) . $data;
+    // Close the final media bracket.
+    if ($media_changes) {
+      $data .= '}';
+    }
+
+    // Add import statements to the top of the stylesheet.
+    $import_string = '';
+    foreach ($import_statements as $values) {
+      if ($media_changes) {
+        foreach ($values[1] as $statement) {
+          $import_string .= str_replace(';', $values[0] . ';', $statement);
+        }
+      }
+      else {
+        $import_string .= implode('', $values[1]);
+      }
+    }
+    $data = $import_string . $data;
   }
 
   // Allow other modules to modify this aggregates contents.
@@ -384,7 +434,7 @@ function advagg_get_js_aggregate_contents($files, $aggregate_settings) {
 
   if (!empty($files)) {
     // Build aggregate JS file.
-    foreach ($files as $filename) {
+    foreach ($files as $filename => $settings) {
       $contents = '';
       // Append a ';' and a newline after each JS file to prevent them from running together.
       if (file_exists($filename)) {
diff --git a/advagg.module b/advagg.module
index 8e8caa2..50fd2fc 100644
--- a/advagg.module
+++ b/advagg.module
@@ -41,6 +41,11 @@ define('ADVAGG_GLOBAL_COUNTER', 0);
  */
 define('ADVAGG_USE_HTTPRL', TRUE);
 
+/**
+ * Combine css files by using media queries instead of media attributes.
+ */
+define('ADVAGG_COMBINE_CSS_MEDIA', TRUE);
+
 // Core hook implementations.
 /**
  * Implements hook_hook_info().
@@ -254,7 +259,7 @@ function advagg_admin_menu_output_alter(&$content) {
 // Core CSS/JS override functions.
 
 /**
- * #pre_render callback so elements can be modifed before they are rendered.
+ * #pre_render callback so elements can be modified before they are rendered.
  *
  * @param $elements
  *   A render array containing:
@@ -286,7 +291,7 @@ function advagg_modify_js_pre_render($elements) {
 }
 
 /**
- * #pre_render callback so elements can be modifed before they are rendered.
+ * #pre_render callback so elements can be modified before they are rendered.
  *
  * @param $elements
  *   A render array containing:
@@ -802,7 +807,7 @@ function advagg_get_current_hooks_hash() {
 }
 
 /**
- * Store settings assoceated with hash.
+ * Store settings associated with hash.
  *
  * @return
  *   value from db_merge
diff --git a/advagg_bundler/advagg_bundler.advagg.inc b/advagg_bundler/advagg_bundler.advagg.inc
index 662a064..ad5e71d 100644
--- a/advagg_bundler/advagg_bundler.advagg.inc
+++ b/advagg_bundler/advagg_bundler.advagg.inc
@@ -28,12 +28,7 @@ function advagg_bundler_advagg_build_aggregate_plans_alter(&$files, &$modified,
     $last_group = '';
     $counter = 0;
     $groupings = array();
-    foreach ($data as $key => $fileinfo) {
-      // Skip non numeric keys.
-      if (!is_numeric($key)) {
-        continue;
-      }
-
+    foreach ($data['files'] as $fileinfo) {
       // Assign each file to their group.
       $group = advagg_bundler_analysis($fileinfo['data']);
       $fileinfo['bundler'] = $group;
diff --git a/advagg_bundler/advagg_bundler.module b/advagg_bundler/advagg_bundler.module
index 3149ee3..0dbbc5a 100644
--- a/advagg_bundler/advagg_bundler.module
+++ b/advagg_bundler/advagg_bundler.module
@@ -75,8 +75,8 @@ function advagg_bundler_init() {
  */
 function advagg_bundler_form_advagg_admin_settings_form_alter(&$form, $form_state) {
   if (advagg_bundler_enabled()) {
-    $form['advagg_core_groups']['#disabled'] = TRUE;
-    $form['advagg_core_groups']['#description'] = t('The bundler submodule disables core grouping logic.');
+    $form['global']['advagg_core_groups']['#disabled'] = TRUE;
+    $form['global']['advagg_core_groups']['#description'] = t('The bundler submodule disables core grouping logic.');
   }
 }
 
