diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 9e9471c..7b4ba11 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,3 +1,8 @@
+ThemeKey 7.x-1.2, 2011-xx-xx
+----------------------------
+[#1215656] mkalkbrenner: Avoid errors if a third party module gets deactivated
+
+
 ThemeKey 7.x-1.1, 2011-06-27
 ----------------------------
 [        ] mkalkbrenner: fixed debug messages in themekey_compat
diff --git a/themekey.module b/themekey.module
index 94e8c8f..76afdfb 100644
--- a/themekey.module
+++ b/themekey.module
@@ -287,3 +287,42 @@ function themekey_cron() {
     themekey_cron_clear_page_cache();
   }
 }
+
+
+/**
+ * Implements hook_modules_disabled().
+ */
+function themekey_modules_disabled($modules) {
+  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/themekey_build.inc';
+
+  // Don't call themekey_rebuild() because the callbacks of disabled modules are still available at this point.
+  // Simply turn off the properties provided for these modules at this point.
+  themekey_scan_modules($modules);
+}
+
+
+/**
+* Implements hook_modules_enabled().
+*/
+function themekey_modules_enabled($modules) {
+  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/themekey_build.inc';
+
+  // Search for new properties provided by a new module
+  themekey_rebuild();
+}
+
+
+/**
+ * Implements hook_flush_caches().
+ *
+ * ThemeKey hijacks this hook to act if a module get updated.
+ */
+function themekey_flush_caches() {
+  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/themekey_build.inc';
+
+  // Search for new properties provided by a new module
+  themekey_rebuild();
+
+  // Don't add a cache table name.
+  return array();
+}
diff --git a/themekey_admin.inc b/themekey_admin.inc
index a696ae4..40a05d7 100644
--- a/themekey_admin.inc
+++ b/themekey_admin.inc
@@ -29,16 +29,12 @@ require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/theme
  * @ingroup forms
  */
 function themekey_rule_chain_form($form, &$form_state) {
-  // BACKPORT
-  if (!empty($form_state['submitted']) || stripos($form_state['method'], 'post') !== FALSE) {
-    // scan for new properties provided by other modules
-    themekey_rebuild();
-  }
-
   $properties = variable_get('themekey_properties', array());
 
   if (empty($properties)) {
-    drupal_goto('admin/config/user-interface/themekey/settings');
+    // first call of this form ever
+    themekey_rebuild();
+    $properties = variable_get('themekey_properties', array());
   }
 
   $themes = themekey_theme_options(TRUE, TRUE);
diff --git a/themekey_base.inc b/themekey_base.inc
index 7da0524..40e1635 100644
--- a/themekey_base.inc
+++ b/themekey_base.inc
@@ -24,13 +24,19 @@
  */
 function themekey_invoke_modules($hook) {
   $return = array();
-  foreach (variable_get('themekey_modules', array('themekey.node')) as $module) {
-    if (is_readable(drupal_get_path('module', 'themekey') . '/modules/' . $module . '.inc')) {
-      require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/modules/' . $module . '.inc';
+  foreach (variable_get('themekey_modules', array('node')) as $module) {
+    if (module_exists($module) && is_readable(drupal_get_path('module', 'themekey') .'/modules/themekey.' . $module .'.inc')) {
+      require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'themekey') . '/modules/themekey.' . $module . '.inc';
+
+      $function = 'themekey_' . $module . '_' . $hook;
+      if (function_exists($function)) {
+        $return = array_merge_recursive($return, $function());
+      }
     }
-    $function = str_replace('.', '_', $module) . '_' . $hook;
-    if (function_exists($function)) {
-      $return = array_merge_recursive($return, $function());
+    else {
+      // seems like a module has been deactivated => update variable 'themekey_modules'
+      module_load_include('inc', 'themekey', 'themekey_build');
+      themekey_scan_modules();
     }
   }
 
diff --git a/themekey_build.inc b/themekey_build.inc
index d70630e..a4624bb 100644
--- a/themekey_build.inc
+++ b/themekey_build.inc
@@ -129,14 +129,17 @@ function themekey_rebuild() {
  *
  * @see themekey_rebuild()
  * @see themekey_invoke_modules()
+ *
+ * @param $blacklist
+ *   array of module names that should not be included
  */
-function themekey_scan_modules() {
+function themekey_scan_modules($blacklist = array()) {
   $modules = array();
-  $files = file_scan_directory(dirname(__FILE__) . '/modules', '/^themekey.[a-z]+.inc$/');
+  $files = file_scan_directory(dirname(__FILE__) . '/modules', '/^themekey\.[a-z]+\.inc$/');
   foreach ($files as $file) {
     list( , $module) = explode('.', $file->name);
-    if (module_exists($module)) {
-      $modules[] = $file->name;
+    if (!in_array($module, $blacklist) && module_exists($module)) {
+      $modules[] = $module;
     }
   }
 
diff --git a/themekey_user_profile.install b/themekey_user_profile.install
index 2d249a9..08a7083 100644
--- a/themekey_user_profile.install
+++ b/themekey_user_profile.install
@@ -11,17 +11,6 @@
 
 
 /**
- * implements hook_install()
- * @return void
- */
-function themekey_user_profile_install() {
-  // rebuild themekey properties
-  module_load_include('inc', 'themekey', 'themekey_build');
-  themekey_rebuild();
-}
-
-
-/**
  * Implements hook_disable().
  */
 function themekey_user_profile_disable() {
