diff --git a/apachesolr.module b/apachesolr.module
index a2feccf..876b574 100644
--- a/apachesolr.module
+++ b/apachesolr.module
@@ -1093,18 +1093,28 @@ function apachesolr_get_solr($env_id = NULL) {
  * @return $environments
  *   The environments in the database
  */
-function apachesolr_load_all_environments() {
+function apachesolr_load_all_environments($reset = FALSE) {
   $environments = &drupal_static(__FUNCTION__);
 
-  if (isset($environments)) {
-    return $environments;
+  if (!$reset) {
+    if (isset($environments)) {
+      return $environments;
+    }
+
+    // Use cache_get to avoid DB when using memcache, etc.
+    $cache = cache_get('apachesolr:environments', 'cache_apachesolr');
+    if (isset($cache->data)) {
+      $environments = $cache->data;
+    }
   }
-  // Use cache_get to avoid DB when using memcache, etc.
-  $cache = cache_get('apachesolr:environments', 'cache_apachesolr');
-  if (isset($cache->data)) {
-    $environments = $cache->data;
+  else {
+    // clear caches we can't skip above
+    if (module_exists('ctools')) {
+      ctools_export_load_object_reset('apachesolr_environment');
+    }
   }
-  elseif (!db_table_exists('apachesolr_index_bundles') || !db_table_exists('apachesolr_environment')) {
+
+  if (!db_table_exists('apachesolr_index_bundles') || !db_table_exists('apachesolr_environment')) {
     // Sometimes this function is called when the 'apachesolr_index_bundles' is
     // not created yet.
     $environments = array();
@@ -1147,8 +1157,8 @@ function apachesolr_load_all_environments() {
  * @return $environment
  *   The environment that was requested or FALSE if non-existent
  */
-function apachesolr_environment_load($env_id) {
-  $environments = apachesolr_load_all_environments();
+function apachesolr_environment_load($env_id, $reset = FALSE) {
+  $environments = apachesolr_load_all_environments($reset);
   return isset($environments[$env_id]) ? $environments[$env_id] : FALSE;
 }
 
@@ -1204,33 +1214,14 @@ function apachesolr_environment_delete($env_id) {
  *
  */
 function apachesolr_environment_clone($env_id) {
-
-  $default = array('env_id' => NULL, 'name' => '', 'url' => '', 'service_class' => '');
   $environment = apachesolr_environment_load($env_id);
   $conf = isset($environment['conf']) ? $environment['conf'] : array();
 
-  //get all environments
   $environments = apachesolr_load_all_environments();
   $new_env_id = apachesolr_create_unique_id($environments, $env_id);
   $environment['env_id'] = $new_env_id;
   $environment['name'] = $environment['name'] . ' [cloned]';
-
-  // Remove any unexpected fields.
-  // @todo - get this from the schema?.
-  $environment = array_intersect_key($environment, $default);
-  db_insert('apachesolr_environment')
-      ->fields($environment)
-      ->execute();
-  // Update the environment variables (if any).
-  foreach ($conf as $name => $value) {
-    db_merge('apachesolr_environment_variable')
-      ->key(array('env_id' => $environment['env_id'], 'name' => $name))
-      ->fields(array('value' => serialize($value)))
-      ->execute();
-  }
-  cache_clear_all('apachesolr:environments', 'cache_apachesolr');
-  drupal_static_reset('apachesolr_load_all_environments');
-  drupal_static_reset('apachesolr_get_solr');
+  apachesolr_environment_save($environment);
 }
 
 /**
@@ -1262,34 +1253,16 @@ function apachesolr_create_unique_id($existing, $id) {
  *
  */
 function apachesolr_environment_save($environment) {
-  $default = array('env_id' => NULL, 'name' => '', 'url' => '', 'service_class' => '');
-
-  $old_environment = apachesolr_environment_load($environment['env_id']);
-  $is_new = FALSE;
-  if (!$old_environment || isset($old_environment['in_code_only'])) {
-    $is_new = TRUE;
-  }
-  // No need to save if there are no changes
-  if ($old_environment == $environment) {
-    // No need to do anything
-    return;
-  }
+  $default = array('env_id' => '', 'name' => '', 'url' => '', 'service_class' => '');
 
   $conf = isset($environment['conf']) ? $environment['conf'] : array();
   // Remove any unexpected fields.
   // @todo - get this from the schema?.
   $environment = array_intersect_key($environment, $default);
-  if ($is_new) {
-    db_insert('apachesolr_environment')
-      ->fields($environment)
-      ->execute();
-  }
-  else {
-    db_update('apachesolr_environment')
-      ->fields($environment)
-      ->condition('env_id', $environment['env_id'])
-      ->execute();
-  }
+  db_merge('apachesolr_environment')
+    ->key(array('env_id' => $environment['env_id']))
+    ->fields($environment)
+    ->execute();
   // Update the environment variables (if any).
   foreach ($conf as $name => $value) {
     db_merge('apachesolr_environment_variable')
@@ -1445,7 +1418,7 @@ function apachesolr_do_query(DrupalSolrQueryInterface $current_query) {
     // A module implementing HOOK_apachesolr_query_alter() aborted the search.
     return array(NULL, array());
   }
-  
+
 
   $keys = $query->getParam('q');
 
