diff --git a/apachesolr.module b/apachesolr.module
index e505c92..cdffcca 100644
--- a/apachesolr.module
+++ b/apachesolr.module
@@ -1093,14 +1093,24 @@ 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__);
 
+  // Reset the static and caches if necessary
+  if ($reset) {
+    $environments = NULL;
+    cache_clear_all('apachesolr:environments', 'cache_apachesolr');
+    if (module_exists('ctools')) {
+      ctools_include('export');
+      ctools_export_load_object_reset('apachesolr_environment');
+    }
+  }
+
   if (isset($environments)) {
     return $environments;
   }
   // Use cache_get to avoid DB when using memcache, etc.
-  $cache = cache_get('apachesolr:environments', 'cache_apachesolr');
+  $cache = $reset ? NULL : cache_get('apachesolr:environments', 'cache_apachesolr');
   if (isset($cache->data)) {
     $environments = $cache->data;
   }
@@ -1110,7 +1120,7 @@ function apachesolr_load_all_environments() {
     $environments = array();
   }
   else {
-    // If ctools is available use it's crud functions to load the environments.
+    // If ctools is available use its crud functions to load the environments.
     if (module_exists('ctools')) {
       ctools_include('export');
       $environments = ctools_export_load_object('apachesolr_environment', 'all');
@@ -1132,7 +1142,6 @@ function apachesolr_load_all_environments() {
     if (!empty($conf_environments)) {
       $environments = drupal_array_merge_deep($environments, $conf_environments);
     }
-
     cache_set('apachesolr:environments', $environments, 'cache_apachesolr');
   }
   return $environments;
@@ -1147,8 +1156,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 +1213,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 +1252,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 +1417,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');
 
