diff --git a/apachesolr.module b/apachesolr.module
index 7dcc8af..a0462e3 100644
--- a/apachesolr.module
+++ b/apachesolr.module
@@ -1160,6 +1160,10 @@ function apachesolr_load_all_environments($reset = FALSE) {
   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)) {
@@ -1302,35 +1306,14 @@ function apachesolr_environment_delete($env_id) {
  *
  */
 function apachesolr_environment_clone($env_id) {
-  static $environments;
-  static $solr_cache;
-
-  $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);
-  $query = "INSERT INTO {apachesolr_environment} (env_id, name, url, service_class) VALUES ('%s', '%s', '%s', '%s')";
-  db_query($query, $environment);
-
-  // Update the environment variables (if any).
-  foreach ($conf as $name => $value) {
-    $query = "INSERT INTO {apachesolr_environment_variable} (env_id, name, value)
-      VALUES ('%s','%s','%s')
-      ON DUPLICATE KEY UPDATE env_id='%s', name='%s', value='%s'";
-    db_query($query, array($environment['env_id'], $name, serialize($value)));
-  }
-  cache_clear_all('apachesolr:environments', 'cache_apachesolr');
-  $environments = NULL;
-  $solr_cache = NULL;
+  apachesolr_environment_save($environment);
 }
 
 /**
@@ -1362,40 +1345,22 @@ function apachesolr_create_unique_id($existing, $id) {
  *
  */
 function apachesolr_environment_save($environment) {
-  static $environments;
-  static $solr_cache;
-  $default = array('env_id' => NULL, 'name' => '', 'url' => '', 'service_class' => '', 'conf' => array('service_class_info' => array()));
-  $old_environment = apachesolr_environment_load($environment['env_id'], $reset = TRUE);
-  $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;
+  // Update or insert since D6 has no db_merge(). Update the environment if it exists.
+  if (db_result(db_query("SELECT 1 FROM {apachesolr_environment} WHERE env_id = '%s'", $environment['env_id']))) {
+    $query = "UPDATE {apachesolr_environment} SET name = '%s', url = '%s', service_class = '%s' WHERE env_id = '%s'";
+    db_query($query, array($environment['name'], $environment['url'], $environment['service_class'], $environment['env_id']));
   }
-
-  // Remove any unexpected fields.
-  // @todo - get this from the schema?.
-  $environment = array_intersect_key($environment, $default);
-  if ($is_new) {
+  else {
     $query = "INSERT INTO {apachesolr_environment} (env_id, name, url, service_class) VALUES ('%s', '%s', '%s', '%s')";
     db_query($query, array($environment['env_id'], $environment['name'], $environment['url'], $environment['service_class']));
   }
-  else {
-    $query = "UPDATE {apachesolr_environment} SET name = '%s', url = '%s', service_class = '%s' WHERE env_id = '%s'";
-    db_query($query, array($environment['name'], $environment['url'], $environment['service_class'], $environment['env_id']));
-  }
   $conf = isset($environment['conf']) ? $environment['conf'] : array();
   // Update the environment variables (if any).
   foreach ($conf as $name => $value) {
-    // Update the environment variables (if any).
     apachesolr_environment_variable_set($environment['env_id'], $name, $value);
   }
-  cache_clear_all('apachesolr:environments', 'cache_apachesolr');
-  $environments = NULL;
-  $solr_cache = NULL;
+  // Reset all caches.
+  apachesolr_load_all_environments(TRUE);
 }
 
 /**
