diff --git a/dmemcache.inc b/dmemcache.inc
index 6a68b14..9c9c253 100644
--- a/dmemcache.inc
+++ b/dmemcache.inc
@@ -34,12 +34,7 @@ function dmemcache_set($key, $value, $exp = 0, $bin = 'cache') {
   $full_key = dmemcache_key($key, $bin);
   $success = FALSE;
   if ($mc = dmemcache_object($bin)) {
-    if ($mc instanceof Memcached) {
-      $success = $mc->set($full_key, $value, $exp);
-    }
-    elseif ($mc instanceof Memcache) {
-      $success = $mc->set($full_key, $value, MEMCACHE_COMPRESSED, $exp);
-    }
+    $success = $mc->set($full_key, $value, $exp);
   }
   $_memcache_statistics[] = array('set', $bin, $full_key, (int)$success);
   return $success;
@@ -75,12 +70,7 @@ function dmemcache_add($key, $value, $exp = 0, $bin = 'cache', $mc = NULL, $flag
   $full_key = dmemcache_key($key, $bin);
   $success = FALSE;
   if ($mc || ($mc = dmemcache_object($bin))) {
-    if ($mc instanceof Memcached) {
-      $success = $mc->add($full_key, $value, $exp);
-    }
-    elseif ($mc instanceof Memcache) {
-      $success = $mc->add($full_key, $value, $flag, $exp);
-    }
+    $success = $mc->add($full_key, $value, $flag, $exp);
   }
   $_memcache_statistics[] = array('add', $bin, $full_key, (int)$success);
   return $success;
@@ -100,19 +90,10 @@ function dmemcache_get($key, $bin = 'cache') {
   $statistics = array('get', $bin, $full_key);
   $success = '0';
   if ($mc = dmemcache_object($bin)) {
-    $track_errors = ini_set('track_errors', '1');
-    $php_errormsg = '';
-
-    $result = @$mc->get($full_key);
+    $result = $mc->get($full_key);
     $statistics[] = (bool) $result;
     $_memcache_statistics[] = $statistics;
 
-    if (!empty($php_errormsg)) {
-      register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
-      $php_errormsg = '';
-    }
-    ini_set('track_errors', $track_errors);
-
     return $result;
   }
 }
@@ -136,21 +117,7 @@ function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) {
   }
   $results = array();
   if ($mc || ($mc = dmemcache_object($bin))) {
-    if ($mc instanceof Memcached) {
-      $results = $mc->getMulti($full_keys);
-    }
-    elseif ($mc instanceof Memcache) {
-      $track_errors = ini_set('track_errors', '1');
-      $php_errormsg = '';
-
-      $results = @$mc->get($full_keys);
-
-      if (!empty($php_errormsg)) {
-        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get_multi: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
-        $php_errormsg = '';
-      }
-      ini_set('track_errors', $track_errors);
-    }
+    $results = $mc->get($full_keys);
   }
   foreach ($statistics as $key => $values) {
     $values[] = isset($results[$key]) ? '1': '0';
@@ -205,26 +172,7 @@ function dmemcache_stats($stats_bin = 'cache', $stats_type = 'default', $aggrega
   foreach ($memcache_bins as $bin => $target) {
     if ($stats_bin == $bin) {
       if ($mc = dmemcache_object($bin)) {
-        if ($mc instanceof Memcached) {
-          $stats[$bin] = $mc->getStats();
-        }
-        // The PHP Memcache extension 3.x version throws an error if the stats
-        // type is NULL or not in {reset, malloc, slabs, cachedump, items,
-        // sizes}. If $stats_type is 'default', then no parameter should be
-        // passed to the Memcache memcache_get_extended_stats() function.
-        else if ($mc instanceof Memcache) {
-          if ($stats_type == 'default' || $stats_type == '') {
-            $stats[$bin] = $mc->getExtendedStats();
-          }
-          // If $slab isn't zero, then we are dumping the contents of a
-          // specific cache slab.
-          else if (!empty($slab))  {
-            $stats[$bin] = $mc->getStats('cachedump', $slab);
-          }
-          else {
-            $stats[$bin] = $mc->getExtendedStats($stats_type);
-          }
-        }
+        $stats[$bin] = $mc->getStats($stats_type, $slab);
       }
     }
   }
@@ -270,24 +218,6 @@ function dmemcache_stats($stats_bin = 'cache', $stats_type = 'default', $aggrega
 function dmemcache_object($bin = NULL, $flush = FALSE) {
   static $extension, $memcacheCache = array(), $memcache_servers, $memcache_bins, $memcache_persistent;
 
-  if (!isset($extension)) {
-    // If an extension is specified in settings.php, use that when available.
-    $preferred = ucfirst(strtolower((variable_get('memcache_extension', NULL))));
-    if (isset($preferred) && class_exists($preferred)) {
-      $extension = $preferred;
-    }
-    // If no extension is set, default to Memcache.
-    // The Memcached extension has some features that the older extension lacks
-    // but also an unfixed bug that affects cache clears.
-    // @see http://pecl.php.net/bugs/bug.php?id=16829
-    elseif (class_exists('Memcache')) {
-      $extension = 'Memcache';
-    }
-    elseif (class_exists('Memcached')) {
-      $extension = 'Memcached';
-    }
-  }
-
   if ($flush) {
     foreach ($memcacheCache as $cluster) {
       $cluster->close();
@@ -316,48 +246,42 @@ function dmemcache_object($bin = NULL, $flush = FALSE) {
       $memcacheCache[$bin] = &$memcacheCache['cache'];
     }
     else {
-      // Create a new Memcache object. Each cluster gets its own Memcache
-      // object.
-      if ($extension == 'Memcached') {
-        $memcache = new Memcached;
-        $default_opts = array(
-          Memcached::OPT_COMPRESSION => FALSE,
-          Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
-        );
-        foreach ($default_opts as $key => $value) {
-          $memcache->setOption($key, $value);
+      if (!isset($extension)) {
+        // If an extension is specified in settings.php, use that when available.
+        $preferred = ucfirst(strtolower((variable_get('memcache_extension', NULL))));
+        if (isset($preferred) && class_exists($preferred)) {
+          $extension = $preferred;
+        }
+        // If no extension is set, default to Memcache.
+        // The Memcached extension has some features that the older extension lacks
+        // but also an unfixed bug that affects cache clears.
+        // @see http://pecl.php.net/bugs/bug.php?id=16829
+        elseif (class_exists('Memcache')) {
+          $extension = 'DMemcache';
         }
-        // See README.txt for setting custom Memcache options when using the
-        // memcached PECL extension.
-        $memconf = variable_get('memcache_options', array());
-        foreach ($memconf as $key => $value) {
-          $memcache->setOption($key, $value);
+        elseif (class_exists('Memcached')) {
+          $extension = 'DMemcached';
         }
       }
-      elseif ($extension == 'Memcache') {
-        $memcache = new Memcache;
+
+      // Create a new Memcache object. Each cluster gets its own Memcache
+      // object.
+      if (!empty($extension)) {
+        $memcache = new $extension();
       }
       else {
+        // throw exception? let fatal error on new?
         drupal_set_message('You must enable the PECL memcached or memcache extension to use memcache.inc.', 'error');
         return;
       }
 
-      // Indicate whether to connect to memcache using a persistent connection.
-      // Note: this only affects the Memcache PECL extension, and does not
-      // affect the Memcached PECL extension.  For a detailed explanation see:
-      //  http://drupal.org/node/822316#comment-4427676
-      if (!isset($memcache_persistent)) {
-        $memcache_persistent = variable_get('memcache_persistent', FALSE);
-      }
-
-      // A variable to track whether we've connected to the first server.
+      // A variable to track whether we've connected to a server.
       $init = FALSE;
 
       // Link all the servers to this cluster.
       foreach ($memcache_servers as $s => $c) {
         if ($c == $cluster) {
           list($host, $port) = explode(':', $s);
-
           // Support unix sockets in the format 'unix:///path/to/socket'.
           if ($host == 'unix') {
             // When using unix sockets use the full path for $host.
@@ -366,35 +290,7 @@ function dmemcache_object($bin = NULL, $flush = FALSE) {
             $port = 0;
           }
 
-          // Using the Memcache PECL extension.
-          if ($memcache instanceof Memcache) {
-            // When using the PECL memcache extension, we must use ->(p)connect
-            // for the first connection.
-            if (!$init) {
-              $track_errors = ini_set('track_errors', '1');
-              $php_errormsg = '';
-
-              if ($memcache_persistent && @$memcache->pconnect($host, $port)) {
-                $init = TRUE;
-              }
-              elseif (!$memcache_persistent && @$memcache->connect($host, $port)) {
-                $init = TRUE;
-              }
-
-              if (!empty($php_errormsg)) {
-                register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_object: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
-                $php_errormsg = '';
-              }
-              ini_set('track_errors', $track_errors);
-            }
-            else {
-              $memcache->addServer($host, $port, $memcache_persistent);
-            }
-          }
-          // Using the Memcached PECL extension.
-          else if ($memcache->addServer($host, $port) && !$init) {
-            $init = TRUE;
-          }
+          $init |= $memcache->addServer($host, $port);
         }
       }
 
@@ -443,3 +339,164 @@ function dmemcache_key($key = '', $bin = 'cache', $reset = FALSE) {
 
   return $full_key;
 }
+
+interface DMemcacheInterface {
+
+  public function __construct();
+
+  public function addServer($host, $port);
+
+  public function set($key, $var, $expire);
+
+  public function add($key, $var, $flag, $expire);
+
+  public function delete($key, $timeout = 0);
+
+  public function flush();
+
+  public function get($key);
+
+  public function getStats($stats_type, $slab);
+}
+
+class DMemcached implements DMemcacheInterface{
+  protected $memcache;
+
+  public function __construct() {
+    $this->memcache = new Memcached();
+    // See README.txt for setting custom Memcache options when using the
+    // memcached PECL extension.
+    $opts = array(
+      Memcached::OPT_COMPRESSION => FALSE,
+      Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
+    ) + variable_get('memcache_options', array());
+    foreach ($opts as $key => $value) {
+      $this->memcache->setOption($key, $value);
+    }
+  }
+
+  public function addServer($host, $port) {
+    return $this->memcache->addServer($host, $port);
+  }
+
+  public function set($key, $var, $expire) {
+    return $this->memcache->set($key, $var, $expire);
+  }
+
+  public function add($key, $var, $flag, $expire) {
+    return $this->memcache->add($key, $var, $expire);
+  }
+
+  public function delete($key, $timeout = 0) {
+    $this->memcache->delete($key, $timeout);
+  }
+
+  public function flush() {
+    $this->memcache->flush();
+  }
+
+  public function get($key) {
+    if (is_array($key)) {
+      return $this->memcache->getMulti($key);
+    }
+
+    return $this->memcache->get($key);
+  }
+
+  public function getStats($stats_type, $slab) {
+    return $this->memcache->getStats();
+  }
+}
+
+class DMemcache implements DMemcacheInterface {
+  protected $memcache;
+  protected $init = FALSE;
+  protected $persistent;
+
+  public function __construct() {
+    $this->memcache = new Memcache();
+
+    // Indicate whether to connect to memcache using a persistent connection.
+    // Note: this only affects the Memcache PECL extension, and does not
+    // affect the Memcached PECL extension.  For a detailed explanation see:
+    //  http://drupal.org/node/822316#comment-4427676
+    $this->persistent = variable_get('memcache_persistent', FALSE);
+  }
+
+  protected function connect($host, $port) {
+    if (!$this->init) {
+      $track_errors = ini_set('track_errors', '1');
+      $php_errormsg = '';
+
+      if ($this->persistent) {
+        $this->init = $this->memcache->pconnect($host, $port);
+      }
+
+      // Catch not persistent or a persistent connection failure and try normal
+      // connect.
+      if (!$this->init) {
+        $this->init = $this->memcache->connect($host, $port);
+      }
+
+      if (!empty($php_errormsg)) {
+        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_object: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
+        $php_errormsg = '';
+      }
+      ini_set('track_errors', $track_errors);
+    }
+
+    return $this->init; // Should this only happen when we actually init?
+  }
+
+  public function addServer($host, $port) {
+    if (!$this->init) {
+      return $this->connect($host, $port);
+    }
+
+    return $this->memcache->addServer($host, $port, $this->persistent);
+  }
+
+  public function set($key, $var, $expire) {
+    return $this->memcache->set($key, $var, MEMCACHE_COMPRESSED, $expire);
+  }
+
+  public function add($key, $var, $flag, $expire) {
+    return $this->memcache->add($key, $var, $flag, $expire);
+  }
+
+  public function delete($key, $timeout = 0) {
+    $this->memcache->delete($key, $timeout);
+  }
+
+  public function flush() {
+    $this->memcache->flush();
+  }
+
+  public function get($key) {
+    $track_errors = ini_set('track_errors', '1');
+    $php_errormsg = '';
+
+    $result = @$this->memcache->get($key);
+
+    if (!empty($php_errormsg)) {
+      register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get_multi: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
+      $php_errormsg = '';
+    }
+    ini_set('track_errors', $track_errors);
+
+    return $result;
+  }
+
+  public function getStats($stats_type, $slab) {
+    if ($stats_type == 'default' || $stats_type == '') {
+      return $this->memcache->getExtendedStats();
+    }
+    // If $slab isn't zero, then we are dumping the contents of a
+    // specific cache slab.
+    elseif (!empty($slab))  {
+      return $this->memcache->getStats('cachedump', $slab);
+    }
+
+    return $this->memcache->getExtendedStats($stats_type);
+  }
+}
