diff --git a/domain_groups.install b/domain_groups.install
index 0082c16..696e99a 100644
--- a/domain_groups.install
+++ b/domain_groups.install
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  * Function naming copied from Stylizer.
  */
 function domain_groups_schema() {
@@ -55,15 +55,15 @@ function domain_groups_schema_1() {
 }
 
 /**
- * Implementation of hook_install().
+ * Implements hook_install().
  */
 function domain_groups_install() {
   drupal_install_schema('domain_groups');
 }
 
 /**
- * Implementation of hook_uninstall().
+ * Implements hook_uninstall().
  */
 function domain_groups_uninstall() {
   drupal_uninstall_schema('domain_groups');
-}
\ No newline at end of file
+}
diff --git a/domain_groups.module b/domain_groups.module
index 84fb49d..d45364f 100644
--- a/domain_groups.module
+++ b/domain_groups.module
@@ -1,68 +1,93 @@
 <?php
 /**
- * Implementation of hook_ctools_plugin_directory().
+ * @file
+ * Contains core functionality for Domain Groups module.
  */
-function domain_groups_ctools_plugin_directory($module, $plugin) {
-  if ($module == 'ctools' && $plugin == 'export_ui') {
-    return 'plugins/' . $plugin;
+
+/**
+ * Include additional files.
+ */
+foreach (module_list() as $module) {
+  if (file_exists($file = dirname(__FILE__) . "/includes/{$module}.inc")) {
+    require_once $file;
   }
 }
 
 /**
+ * Implements hook_theme().
+ */
+function domain_groups_theme($existing, $type, $theme, $path) {
+  return array(
+    'domain_groups_domain_list' => array(
+      'variables' => array(
+        'domains' => NULL,
+        'format' => NULL
+      ),
+    ),
+  );
+}
+
+/**
  * Load function.
  */
 function domain_groups_load_group($name) {
-  $group = db_fetch_object(db_query("SELECT * FROM domain_groups WHERE name = '%s'", $name));
-
-  $group->domains = unserialize($group->domains);
-
-  return $group;
+  $domain_group = ctools_export_crud_load('domain_groups', $name);
+  $domain_group->domains = unserialize($domain_group->domains);
+  return $domain_group;
 }
 
 /**
  * Returns available groups.
  * @return array().
- * @TODO - check state (enabled/disabled).
  */
-function domain_groups_groups() {
-  $result = db_query("SELECT * FROM domain_groups");
+function domain_groups_groups($reset = FALSE) {
+  static $groups = array();
 
-  $groups = array();
-  while ($group = db_fetch_object($result)) {
-    $groups[$group->name] = array(
-      'description' => $group->description,
-      'domains' => domain_groups_domain_list(unserialize($group->domains)),
-    );
+  if (empty($groups) || $reset) {
+    foreach (ctools_export_crud_load_all('domain_groups') as $domain_group) {
+      if (!isset($domain_group->disabled) || $domain_group->disabled == FALSE) {
+        $groups[$domain_group->name] = array(
+          'description' => $domain_group->description,
+          'domains' => domain_groups_domain_list(unserialize($domain_group->domains)),
+        );
+      }
+    }
   }
 
   return $groups;
 }
 
 /**
- * Maps a saved array of ID's to domain sitename.
- * @return array().
+ * Returns groups based on selected domains.
  */
-function domain_groups_domain_list($domain_ids) {
-	$domains = domain_domains();
-	$options = array();
-	foreach ($domain_ids AS $id) {
-		if ($id == -1) {
-			$id = 0;
-		}
-  	$options[$id] = check_plain($domains[$id]['sitename']);
+function domain_groups_groups_from_domains($domains) {
+  $domain_groups = array();
+
+  $domains = domain_groups_domain_list($domains);
+  foreach (domain_groups_groups() as $name => $domain_group) {
+    $diff = array_diff($domain_group['domains'], $domains);
+    if (empty($diff)) {
+      $domain_groups[$name] = $domain_group;
+    }
   }
-  return $options;
+
+  return $domain_groups;
 }
 
 /**
- * Implementation of hook_theme().
+ * Maps a saved array of ID's to domain sitename.
+ * @return array().
  */
-function domain_groups_theme($existing, $type, $theme, $path) {
-	return array(
-		'domain_groups_domain_list' => array(
-			'variables' => array('domains' => NULL, 'format' => NULL),
-		),
-	);
+function domain_groups_domain_list($domain_ids) {
+  $domains = domain_domains();
+  $options = array();
+  foreach ($domain_ids AS $id) {
+    if ($id == -1) {
+      $id = 0;
+    }
+    $options[$id] = check_plain($domains[$id]['sitename']);
+  }
+  return $options;
 }
 
 /**
@@ -72,18 +97,18 @@ function domain_groups_theme($existing, $type, $theme, $path) {
  * $variables['format'] - output format. Can only be ul for now.
  */
 function theme_domain_groups_domain_list($variables) {
-	$domains = $variables['domains'];
-	$format = $variables['format'];
+  $domains = $variables['domains'];
+  $format = $variables['format'];
 
-	switch ($format) {
-		case 'ul':
-			$output = '<ul>';
-			foreach ($domains AS $domain) {
-				$output .= '<li>' . check_plain($domain) . '</li>';
-			}
-			$output .= '</ul>';
-			break;
-	}
+  switch ($format) {
+    case 'ul':
+      $output = '<ul>';
+      foreach ($domains AS $domain) {
+        $output .= '<li>' . check_plain($domain) . '</li>';
+      }
+      $output .= '</ul>';
+      break;
+  }
 
-	return $output;
-}
\ No newline at end of file
+  return $output;
+}
diff --git a/domain_groups_mappings/domain_groups_mappings.info b/domain_groups_mappings/domain_groups_mappings.info
deleted file mode 100644
index 77836bd..0000000
--- a/domain_groups_mappings/domain_groups_mappings.info
+++ /dev/null
@@ -1,5 +0,0 @@
-name = Domain Groups Mappings
-description = Adds ability to map specific Domain Groups to certain domain.
-core = 6.x
-package = Domain Access
-dependencies[] = domain_groups
\ No newline at end of file
diff --git a/domain_groups_mappings/domain_groups_mappings.install b/domain_groups_mappings/domain_groups_mappings.install
deleted file mode 100644
index 9b4238f..0000000
--- a/domain_groups_mappings/domain_groups_mappings.install
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-/**
- * Implementation of hook_schema().
- */
-function domain_groups_mappings_schema() {
-	$schema['domain_groups_mappings'] = array(
-    'fields' => array(
-      'domainid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'description' => 'Domain ID these groups are mapped to.',
-      ),
-      'groups' => array(
-        'type' => 'text',
-        'description' => 'Groups mapped to this domain id.',
-      ),
-    ),
-  );
-
-  return $schema;
-}
-
-function domain_groups_mappings_install() {
-  drupal_install_schema('domain_groups_mappings');
-}
-
-function domain_groups_mappings_uninstall() {
-  drupal_uninstall_schema('domain_groups_mappings');
-}
\ No newline at end of file
diff --git a/domain_groups_mappings/domain_groups_mappings.module b/domain_groups_mappings/domain_groups_mappings.module
deleted file mode 100644
index e95c448..0000000
--- a/domain_groups_mappings/domain_groups_mappings.module
+++ /dev/null
@@ -1,130 +0,0 @@
-<?php
-/**
- * @file
- * Main module file for Domain Groups Mappings, which lets domains be coupled 
- * with one or more groups, defined by Domain Groups.
- */
-
-/**
- * Implementation of hook_form_alter().
- * Adds Groups form to domain add/edit and domain conf.
- */
-function domain_groups_mappings_form_alter(&$form, &$form_state, $form_id) {
-  if ($form_id == 'domain_form' || $form_id == 'domain_configure_form') {
-    $domain_id = $form['#domain']['domain_id'];
-    if ($form_id == 'domain_configure_form') {
-      $domain_id = 0;
-    }
-    
-    $saved_groups = array();
-    if (!is_null($domain_id)) {
-      $db_groups = domain_groups_mappings_get_groups_by_domain($domain_id);
-      if (!empty($db_groups->groups)) {
-        foreach ($db_groups->groups AS $group) {
-          $saved_groups[] = $group;
-        }
-      }
-    }
-
-    $options = array();
-    $groups = domain_groups_groups();
-    foreach ($groups AS $group_name => $group) {
-      $options[$group_name] = $group['description'];
-    }
-
-    if ($form_id == 'domain_form') {
-      $form['submit']['#weight'] = 2;
-
-      $form['domain_groups'] = array(
-        '#type' => 'fieldset',
-        '#title' => 'Domain Groups',
-        '#weight' => 1,
-      );
-
-      $fieldset = 'domain_groups';
-    }
-    elseif ($form_id == 'domain_configure_form') {
-      $fieldset = 'domain';
-      // Workaround, see below.
-      $form['#submit'][] = 'domain_groups_mappings_conf_submit';
-    }
-
-    $form[$fieldset]['domain_groups_mappings'] = array(
-      '#type' => 'select',
-      '#multiple' => TRUE,
-      '#default_value' => $saved_groups,
-      '#options' => $options,
-      '#title' => t('Domain Groups mappings.'),
-      '#description' => t('Choose groups to map this domain to.'),
-    );
-  }
-}
-
-/**
- * Workaround since $form_state isn't sent to hook_domainupdate from
- * domain_configure_form.
- */
-function domain_groups_mappings_conf_submit($form, &$form_state) {
-  if (!empty($form_state['values']['domain_groups_mappings'])) {
-    $variables = array(
-      'domainid' => 0,
-      'groups' => $form_state['values']['domain_groups_mappings'],
-    );
-    domain_groups_mappings_save_mappings($variables);
-  }
-}
-
-/**
- * Implementation of hook_domainupdate().
- */
-function domain_groups_mappings_domainupdate($op, $domain, $form_state = array()) {
-  switch ($op) {
-    case 'create':
-    case 'update':
-      if (!empty($form_state['values']['domain_groups_mappings'])) {
-        $variables = array(
-          'domainid' => $domain['domain_id'],
-          'groups' => $form_state['values']['domain_groups_mappings'],
-        );
-        domain_groups_mappings_save_mappings($variables);
-      }
-      break;
-    case 'delete':
-      db_query("DELETE FROM {domain_groups_mappings} WHERE domainid = %d", $domain['domain_id']);
-      break;
-  }
-}
-
-/**
- * API function that saves mappings.
- * @param $variables
- */
-function domain_groups_mappings_save_mappings($variables = array()) {
-  if (!isset($variables['domainid'])) {
-    return;
-  }
-
-  if (!empty($variables)) {
-    // Check if record allready exists for this domain.
-    $count = db_result(db_query('SELECT COUNT(*) FROM {domain_groups_mappings} WHERE domainid = %d', $variables['domainid']));
-    if ($count > 0) {
-      db_query("UPDATE {domain_groups_mappings} SET groups = '%s' WHERE domainid = '%d'", serialize($variables['groups']), $variables['domainid']);
-    }
-    // Otherwise, save new mapping.
-    else {
-      db_query("INSERT INTO {domain_groups_mappings} VALUES('%d', '%s')", $variables['domainid'], serialize($variables['groups']));
-    }
-  }
-}
-
-/**
- * Load function that returns groups for one domain.
- * @param $domain_id
- */
-function domain_groups_mappings_get_groups_by_domain($domain_id) {
-  $groups = db_fetch_object(db_query("SELECT * FROM {domain_groups_mappings} WHERE domainid = %d", $domain_id));
-
-  $groups->groups = unserialize($groups->groups);
-
-  return $groups;
-}
\ No newline at end of file
diff --git a/includes/ctools.inc b/includes/ctools.inc
new file mode 100644
index 0000000..3b1093b
--- /dev/null
+++ b/includes/ctools.inc
@@ -0,0 +1,14 @@
+<?php
+/**
+ * @file
+ * Chaos Tool suite module integration.
+ */
+
+/**
+ * Implements hook_ctools_plugin_directory().
+ */
+function domain_groups_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'ctools' && $plugin == 'export_ui') {
+    return "plugins/{$plugin}";
+  }
+}
diff --git a/modules/domain_groups_mappings/domain_groups_mappings.info b/modules/domain_groups_mappings/domain_groups_mappings.info
new file mode 100644
index 0000000..77836bd
--- /dev/null
+++ b/modules/domain_groups_mappings/domain_groups_mappings.info
@@ -0,0 +1,5 @@
+name = Domain Groups Mappings
+description = Adds ability to map specific Domain Groups to certain domain.
+core = 6.x
+package = Domain Access
+dependencies[] = domain_groups
\ No newline at end of file
diff --git a/modules/domain_groups_mappings/domain_groups_mappings.install b/modules/domain_groups_mappings/domain_groups_mappings.install
new file mode 100644
index 0000000..1dd8a47
--- /dev/null
+++ b/modules/domain_groups_mappings/domain_groups_mappings.install
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Implements hook_schema().
+ */
+function domain_groups_mappings_schema() {
+  $schema['domain_groups_mappings'] = array(
+    'fields' => array(
+      'domainid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => 'Domain ID these groups are mapped to.',
+      ),
+      'groups' => array(
+        'type' => 'text',
+        'description' => 'Groups mapped to this domain id.',
+      ),
+    ),
+  );
+
+  return $schema;
+}
+
+function domain_groups_mappings_install() {
+  drupal_install_schema('domain_groups_mappings');
+}
+
+function domain_groups_mappings_uninstall() {
+  drupal_uninstall_schema('domain_groups_mappings');
+}
diff --git a/modules/domain_groups_mappings/domain_groups_mappings.module b/modules/domain_groups_mappings/domain_groups_mappings.module
new file mode 100644
index 0000000..0abfcfe
--- /dev/null
+++ b/modules/domain_groups_mappings/domain_groups_mappings.module
@@ -0,0 +1,130 @@
+<?php
+/**
+ * @file
+ * Main module file for Domain Groups Mappings, which lets domains be coupled
+ * with one or more groups, defined by Domain Groups.
+ */
+
+/**
+ * Implements hook_form_alter().
+ * Adds Groups form to domain add/edit and domain conf.
+ */
+function domain_groups_mappings_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'domain_form' || $form_id == 'domain_configure_form') {
+    $domain_id = $form['#domain']['domain_id'];
+    if ($form_id == 'domain_configure_form') {
+      $domain_id = 0;
+    }
+
+    $saved_groups = array();
+    if (!is_null($domain_id)) {
+      $db_groups = domain_groups_mappings_get_groups_by_domain($domain_id);
+      if (!empty($db_groups->groups)) {
+        foreach ($db_groups->groups AS $group) {
+          $saved_groups[] = $group;
+        }
+      }
+    }
+
+    $options = array();
+    $groups = domain_groups_groups();
+    foreach ($groups AS $group_name => $group) {
+      $options[$group_name] = $group['description'];
+    }
+
+    if ($form_id == 'domain_form') {
+      $form['submit']['#weight'] = 2;
+
+      $form['domain_groups'] = array(
+        '#type' => 'fieldset',
+        '#title' => 'Domain Groups',
+        '#weight' => 1,
+      );
+
+      $fieldset = 'domain_groups';
+    }
+    elseif ($form_id == 'domain_configure_form') {
+      $fieldset = 'domain';
+      // Workaround, see below.
+      $form['#submit'][] = 'domain_groups_mappings_conf_submit';
+    }
+
+    $form[$fieldset]['domain_groups_mappings'] = array(
+      '#type' => 'select',
+      '#multiple' => TRUE,
+      '#default_value' => $saved_groups,
+      '#options' => $options,
+      '#title' => t('Domain Groups mappings.'),
+      '#description' => t('Choose groups to map this domain to.'),
+    );
+  }
+}
+
+/**
+ * Workaround since $form_state isn't sent to hook_domainupdate from
+ * domain_configure_form.
+ */
+function domain_groups_mappings_conf_submit($form, &$form_state) {
+  if (!empty($form_state['values']['domain_groups_mappings'])) {
+    $variables = array(
+      'domainid' => 0,
+      'groups' => $form_state['values']['domain_groups_mappings'],
+    );
+    domain_groups_mappings_save_mappings($variables);
+  }
+}
+
+/**
+ * Implements hook_domainupdate().
+ */
+function domain_groups_mappings_domainupdate($op, $domain, $form_state = array()) {
+  switch ($op) {
+    case 'create':
+    case 'update':
+      if (!empty($form_state['values']['domain_groups_mappings'])) {
+        $variables = array(
+          'domainid' => $domain['domain_id'],
+          'groups' => $form_state['values']['domain_groups_mappings'],
+        );
+        domain_groups_mappings_save_mappings($variables);
+      }
+      break;
+    case 'delete':
+      db_query("DELETE FROM {domain_groups_mappings} WHERE domainid = %d", $domain['domain_id']);
+      break;
+  }
+}
+
+/**
+ * API function that saves mappings.
+ * @param $variables
+ */
+function domain_groups_mappings_save_mappings($variables = array()) {
+  if (!isset($variables['domainid'])) {
+    return;
+  }
+
+  if (!empty($variables)) {
+    // Check if record allready exists for this domain.
+    $count = db_result(db_query('SELECT COUNT(*) FROM {domain_groups_mappings} WHERE domainid = %d', $variables['domainid']));
+    if ($count > 0) {
+      db_query("UPDATE {domain_groups_mappings} SET groups = '%s' WHERE domainid = '%d'", serialize($variables['groups']), $variables['domainid']);
+    }
+    // Otherwise, save new mapping.
+    else {
+      db_query("INSERT INTO {domain_groups_mappings} VALUES('%d', '%s')", $variables['domainid'], serialize($variables['groups']));
+    }
+  }
+}
+
+/**
+ * Load function that returns groups for one domain.
+ * @param $domain_id
+ */
+function domain_groups_mappings_get_groups_by_domain($domain_id) {
+  $groups = db_fetch_object(db_query("SELECT * FROM {domain_groups_mappings} WHERE domainid = %d", $domain_id));
+
+  $groups->groups = unserialize($groups->groups);
+
+  return $groups;
+}
diff --git a/plugins/export_ui/domain_groups_ui.class.php b/plugins/export_ui/domain_groups_ui.class.php
index 8b90e00..48a2046 100644
--- a/plugins/export_ui/domain_groups_ui.class.php
+++ b/plugins/export_ui/domain_groups_ui.class.php
@@ -3,8 +3,8 @@
  * UI class for Domain Groups.
  */
 class domain_groups_ui extends ctools_export_ui {
-	
-	function list_sort_options() {
+
+  function list_sort_options() {
     return array(
       'disabled' => t('Enabled, title'),
       'name' => t('Name'),
@@ -12,7 +12,7 @@ class domain_groups_ui extends ctools_export_ui {
     );
   }
 
-	function list_build_row($item, &$form_state, $operations) {
+  function list_build_row($item, &$form_state, $operations) {
     // Set up sorting
     switch ($form_state['values']['order']) {
       case 'disabled':
@@ -48,4 +48,4 @@ class domain_groups_ui extends ctools_export_ui {
       array('data' => t('Operations'),  'class' => 'ctools-export-ui-ops'),
     );
   }
-}
\ No newline at end of file
+}
