diff --git a/db-config.php b/db-config.php
index cebe3a82ff1d7ae10265708297080e529acc6955..3b0e0246e074078c340bbc467e22b80b4cc53f50 100644
--- a/db-config.php
+++ b/db-config.php
@@ -1,9 +1,14 @@
 <?php
 
+// Common settings
+$wpdb->save_queries = false;
+$wpdb->persistent = false;
+$wpdb->max_connections = 30;
+
 define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends");
 
-require_once('r2db/db-hash.php');
-require_once('r2db/db-backends.php');
+include_once('r2db/db-hash.php');
+include_once('r2db/db-backends.php');
 
 // Add the global database (configured in wp-config.php), stores the global
 // blogs and users tables.
@@ -16,5 +21,5 @@ $wpdb->add_database(array(
     "write" => 1, "read" => 1, "timeout" => 2
     ));
 
-$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb_hash);
+$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map);
 
diff --git a/r2db/db-backends.php b/r2db/db-backends.php
index af791645d30e4c4d427e4c16e89162d652f5db3d..d7eafc9777ae0e824ced54fb59e768b5b921da74 100644
--- a/r2db/db-backends.php
+++ b/r2db/db-backends.php
@@ -1,6 +1,6 @@
 <?php
 
-function noblogs_load_backends($db_config_file, &$hashptr) {
+function noblogs_load_backends($db_config_file, $hashptr) {
   global $wpdb;
 
   $backend_map = array();
diff --git a/r2db/db-hash.php b/r2db/db-hash.php
index 2c14ce2573ad0c02dd4366ba2b98ea57a2fe80db..6f69aaf002eadcf096242e89d463c025fb0420d4 100644
--- a/r2db/db-hash.php
+++ b/r2db/db-hash.php
@@ -1,15 +1,15 @@
 <?php
 
-require_once('flexihash.php');
+require_once(dirname(__FILE__) . '/flexihash.php');
 
-$wpdb_hash = new Flexihash();
+$wpdb->hash_map = new Flexihash();
 
 /* Hashing function to map blogs to databases.
  *
  * Implements a consistent hashing scheme using Flexihash.
  */
 function noblogs_db_callback($query, $wpdb) {
-  global $wpdb_hash;
+  $wpdb_hash = $wpdb->hash_map;
   if (preg_match("/^{$wpdb->base_prefix}(\d+)_/", $wpdb->table, $matches)) {
     $blog_id = $matches[1];
     return $wpdb_hash->lookup($blog_id);
diff --git a/r2db/tools/db-migrate-to-new-schema.php b/r2db/tools/db-migrate-to-new-schema.php
index e393de01981a75c4e2a7307d27da0a9ce25cd677..b1083e725bf3f540b4a783505aaf5f87b4240676 100755
--- a/r2db/tools/db-migrate-to-new-schema.php
+++ b/r2db/tools/db-migrate-to-new-schema.php
@@ -5,7 +5,7 @@
 define('WP_CACHE',false);
 /** Setup WordPress environment */
 require_once('wp-load.php');
-require_once('db-config-new.php');
+require_once('db-config.php');
 
 
 function old_hash($dbid) {
@@ -24,11 +24,10 @@ function old_hash($dbid) {
   }
 }
 
-function new_hash($dbid) {
-  global $wpdb_hash;
-  global $wpdb_reverse_backend_map;
-  $lookup = $wpdb_hash->lookup($dbid);
-  $backend = $wpdb_reverse_backend_map[$lookup];
+function new_hash($dbid, $reversemap) {
+  global $wpdb;
+  $lookup = $wpdb->hash_map->lookup($dbid);
+  $backend = $reversemap[$lookup];
   $result = array();
   if (preg_match('/^(.*):([0-9]*)$/', $backend['host'], $matches)) {
     $result['host'] = $matches[1];
@@ -54,11 +53,15 @@ function get_all_blogs()
 {
   global $wpdb;
 
-  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path";
+//  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path";
+  $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 0 ORDER BY domain, path";
   $result = $wpdb->get_results($sql);
   return ($result);
 }
 
+$hashmap = new Flexihash();
+$reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $hashmap);
+
 $new_counts = array();
 $moved_count = 0;
 $blogs = get_all_blogs();
@@ -67,7 +70,7 @@ foreach ($blogs as $blog) {
 
   $old_params = old_hash($blog_id);
   $old_dburi = mysqlurl($old_params);
-  $new_params = new_hash($blog_id);
+  $new_params = new_hash($blog_id, $reverse_backend_map);
   $new_dburi = mysqlurl($new_params);
 
   if ($new_counts[$new_params['host']]) {
diff --git a/wp-content/db.php b/wp-content/db.php
index f900d305d761ac154d73397559c736c64ec7cfd8..f819e22502bb8043b19bd06a9944b21d7bffef79 100644
--- a/wp-content/db.php
+++ b/wp-content/db.php
@@ -51,6 +51,13 @@ define( 'HYPERDB_CONN_HOST_ERROR', 2003 );   // Can't connect to MySQL server on
 define( 'HYPERDB_SERVER_GONE_ERROR', 2006 ); // MySQL server has gone away
 
 class hyperdb extends wpdb {
+
+        /**
+	 * A/I patch! 
+	 * store a FlexiHash() instance here.
+	 */
+	var $hash_map;
+
 	/**
 	 * The last table that was queried
 	 * @var string