diff --git a/apachesolr.install b/apachesolr.install
index 5a73825..a662b06 100644
--- a/apachesolr.install
+++ b/apachesolr.install
@@ -374,3 +374,23 @@ function apachesolr_update_6303() {
   variable_set('apachesolr_update_from_6303', TRUE);
   return array();
 }
+
+/**
+* Turn global variables in environment specific ones.
+*/
+function apachesolr_update_6304() {
+  $stored = variable_get('apachesolr_index_last', array());
+  foreach ($stored as $env_id => $entity_info) {
+    apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', $entity_info);
+  }
+  variable_del('apachesolr_index_last');
+
+  $updated = variable_get('apachesolr_index_updated', array());
+  $optimized = variable_get('apachesolr_last_optimize', 0);
+  foreach ($updated as $env_id => $timestamp) {
+    apachesolr_environment_variable_set($env_id, 'apachesolr_index_updated', $timestamp);
+    apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize', $optimized);
+  }
+  variable_del('apachesolr_index_updated');
+  variable_del('apachesolr_last_optimize');
+}
diff --git a/apachesolr.module b/apachesolr.module
index c6d0ca1..c893048 100644
--- a/apachesolr.module
+++ b/apachesolr.module
@@ -849,34 +849,39 @@ function apachesolr_set_stats_message($text, $type = 'status', $repeat = FALSE)
  * Returns last changed and last ID for an environment and entity type.
  */
 function apachesolr_get_last_index_position($env_id, $entity_type) {
-  $stored = variable_get('apachesolr_index_last', array());
-  return isset($stored[$env_id][$entity_type]) ? $stored[$env_id][$entity_type] : array('last_changed' => 0, 'last_entity_id' => 0);
+  $stored = apachesolr_environment_variable_get($env_id, 'apachesolr_index_last', array());
+  return isset($stored[$entity_type]) ? $stored[$entity_type] : array('last_changed' => 0, 'last_entity_id' => 0);
 }
 
 /**
  * Sets last changed and last ID for an environment and entity type.
  */
 function apachesolr_set_last_index_position($env_id, $entity_type, $last_changed, $last_entity_id) {
-  $stored = variable_get('apachesolr_index_last', array());
-  $stored[$env_id][$entity_type] = array('last_changed' => $last_changed, 'last_entity_id' => $last_entity_id);
-  variable_set('apachesolr_index_last', $stored);
+  $stored = apachesolr_environment_variable_get($env_id, 'apachesolr_index_last', array());
+  $stored[$entity_type] = array('last_changed' => $last_changed, 'last_entity_id' => $last_entity_id);
+  apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', $stored);
 }
 
 /**
  * Clear a specific environment, or clear all.
  */
 function apachesolr_clear_last_index_position($env_id = NULL, $entity_type = NULL) {
-  $stored = variable_get('apachesolr_index_last', array());
-  if (empty($env_id)) {
-    $stored = array();
-  }
-  elseif ($entity_type) {
-    unset($stored[$env_id][$entity_type]);
+  if (!empty($env_id)) {
+    $stored = apachesolr_environment_variable_get($env_id, 'apachesolr_index_last', array());
+    if ($entity_type) {
+      unset($stored[$entity_type]);
+    }
+    else {
+      $stored = array();
+    }
+    apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', $stored);
   }
   else {
-    unset($stored[$env_id]);
+    $environments = apachesolr_load_all_environments();
+    foreach (array_keys($environments) as $env_id) {
+      apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', array());
+    }
   }
-  variable_set('apachesolr_index_last', $stored);
 }
 
 /**
@@ -885,17 +890,7 @@ function apachesolr_clear_last_index_position($env_id = NULL, $entity_type = NUL
  *   A timestamp or zero. If zero, the variable is deleted.
  */
 function apachesolr_set_last_index_updated($env_id, $timestamp = 0) {
-  $updated = variable_get('apachesolr_index_updated', array());
-  if (!is_array($updated)) {
-    $updated = array();
-  }
-  if ($timestamp > 0) {
-    $updated[$env_id] = $timestamp;
-  }
-  else {
-    unset($updated[$env_id]);
-  }
-  variable_set('apachesolr_index_updated', $updated);
+  apachesolr_environment_variable_set($env_id, 'apachesolr_index_updated', $timestamp);
 }
 
 /**
@@ -903,8 +898,7 @@ function apachesolr_set_last_index_updated($env_id, $timestamp = 0) {
  * @return integer (timestamp)
  */
 function apachesolr_get_last_index_updated($env_id) {
-  $updated = variable_get('apachesolr_index_updated', array());
-  return isset($updated[$env_id]) ? $updated[$env_id] : 0;
+  return apachesolr_environment_variable_get($env_id, 'apachesolr_index_updated', 0);
 }
 
 /**
@@ -925,10 +919,11 @@ function apachesolr_cron($env_id = NULL) {
 
   // Optimize the index (by default once a day).
   $optimize_interval = variable_get('apachesolr_optimize_interval', 60 * 60 * 24);
-  $last = variable_get('apachesolr_last_optimize', 0);
   $time = APACHESOLR_REQUEST_TIME;
 
   foreach($environments as $env_id) {
+    $last = apachesolr_environment_variable_get($env_id, 'apachesolr_last_optimize', 0);
+
     // Indexes in read-only mode do not change the index, so will not update, delete, or optimize during cron.
     if (apachesolr_environment_variable_get($env_id, 'apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
       continue;
@@ -953,7 +948,7 @@ function apachesolr_cron($env_id = NULL) {
       $solr = apachesolr_get_solr($env_id);
       if ($optimize_interval && ($time - $last > $optimize_interval)) {
         $solr->optimize(FALSE, FALSE);
-        variable_set('apachesolr_last_optimize', $time);
+        apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize', $time);
         apachesolr_set_last_index_updated($env_id, $time);
       }
       // Only clear the cache if the index changed.
diff --git a/apachesolr_access/tests/apachesolr_access.test b/apachesolr_access/tests/apachesolr_access.test
index b6b5fb3..b52f4be 100644
--- a/apachesolr_access/tests/apachesolr_access.test
+++ b/apachesolr_access/tests/apachesolr_access.test
@@ -138,10 +138,16 @@ class DrupalApacheSolrNodeAccess extends DrupalWebTestCase {
     $env_id = apachesolr_default_environment(NULL, TRUE);
     apachesolr_set_last_index_position($env_id, 'node', 1, 1);
     $empty = serialize(array());
-    $value = db_result(db_query('SELECT value FROM {variable} WHERE name = "%s"', array('apachesolr_index_last')));
+    $value = db_result(
+      db_query("SELECT value FROM {apachesolr_environment_variable} WHERE env_id = '%s' AND name = '%s'",
+      $env_id, 'apachesolr_index_last')
+    );
     $this->assertNotEqual($value, $empty, 'value is not empty array');
     $this->drupalPost('admin/content/node-settings', array(), t('Rebuild permissions'));
-    $value = db_result(db_query('SELECT value FROM {variable} WHERE name = "%s"', array('apachesolr_index_last')));
+    $value = db_result(
+      db_query("SELECT value FROM {apachesolr_environment_variable} WHERE env_id = '%s' AND name = '%s'",
+        $env_id, 'apachesolr_index_last')
+    );
     $this->assertEqual($value, $empty, 'value is empty array');
   }
 }
