diff --git a/utf8mb4_convert.drush.inc b/utf8mb4_convert.drush.inc
index b102dc2..f51cd16 100644
--- a/utf8mb4_convert.drush.inc
+++ b/utf8mb4_convert.drush.inc
@@ -23,6 +23,13 @@ class DrupalCharsetConverter {
    */
   protected $collation = 'utf8mb4_general_ci';
 
+  /**
+   * The current database connection for all actions.
+   *
+   * @var DatabaseConnection
+   */
+  protected $connection;
+
   public function __construct($charset = NULL, $collation = NULL) {
     if ($charset) {
       $this->charset = $charset;
@@ -33,6 +40,16 @@ class DrupalCharsetConverter {
   }
 
   /**
+   * Set the active database connection.
+   *
+   * @param DatabaseConnection $connection
+   *   The database connection as retrieved by Database::getConnection().
+   */
+  public function setConnection(DatabaseConnection $connection) {
+    $this->connection = $connection;
+  }
+
+  /**
    * Convert the MySQL drupal databases character set and collation.
    *
    * @param array $databases
@@ -46,8 +63,8 @@ class DrupalCharsetConverter {
         if (strpos($database['driver'], 'mysql') === 0) {
           drush_print('Target MySQL database: ' . $database['database'] . '@' . $database['host'] . ' (' . $database_key . ':' . $target  . ')' );
           // Connect to next database.
-          db_set_active($database_key);
-          $connection = Database::getConnection();
+          $connection = Database::getConnection($target, $database_key);
+          $this->setConnection($connection);
           // Check the database type is mysql.
           $db_type = $connection->databaseType();
           // Skip if not MySQL.
@@ -67,8 +84,7 @@ class DrupalCharsetConverter {
         }
       }
     }
-    // Reset connection to default.
-    db_set_active();
+
     return $success;
   }
 
@@ -86,7 +102,7 @@ class DrupalCharsetConverter {
   public function convertDatabase($database_name, $charset = NULL, $collation = NULL) {
     drush_print('Converting database: ' . $database_name);
     $sql = "ALTER DATABASE `" . $database_name . "` CHARACTER SET = :charset COLLATE = :collation;";
-    return db_query($sql, array(
+    return $this->connection->query($sql, array(
       ':charset' => $charset ?: $this->charset,
       ':collation' => $collation ?: $this->collation,
     ));
@@ -110,7 +126,7 @@ class DrupalCharsetConverter {
     $table_names = array_keys($schema);
     sort($table_names);
     foreach ($table_names as $table_name) {
-      if (!db_table_exists($table_name)) {
+      if (!$this->connection->schema()->tableExists($table_name)) {
         continue;
       }
       $this->convertTable($table_name, $charset, $collation);
@@ -131,15 +147,15 @@ class DrupalCharsetConverter {
    *   success|failure.
    */
   public function convertTable($table_name, $charset = NULL, $collation = NULL) {
-    db_query("ALTER TABLE {" . $table_name . "} ROW_FORMAT=DYNAMIC ENGINE=INNODB");
+    $this->connection->query("ALTER TABLE {" . $table_name . "} ROW_FORMAT=DYNAMIC ENGINE=INNODB");
     $sql = "ALTER TABLE {" . $table_name . "} CHARACTER SET = :charset COLLATE = :collation";
     drush_print('Converting table: ' . $table_name);
-    $result = db_query($sql, array(
+    $result = $this->connection->query($sql, array(
       ':charset' => $charset ?: $this->charset,
       ':collation' => $collation ?: $this->collation,
     ));
     $this->convertTableFields($table_name, $charset, $collation);
-    db_query("OPTIMIZE TABLE {" . $table_name . "}");
+    $this->connection->query("OPTIMIZE TABLE {" . $table_name . "}");
     return $result;
   }
 
@@ -157,7 +173,7 @@ class DrupalCharsetConverter {
    *   success|failure.
    */
   public function convertTableFields($table_name, $charset = NULL, $collation = NULL) {
-    $results = db_query("SHOW FULL FIELDS FROM {" . $table_name . "}")->fetchAllAssoc('Field');
+    $results = $this->connection->query("SHOW FULL FIELDS FROM {" . $table_name . "}")->fetchAllAssoc('Field');
     $charset = $charset ?: $this->charset;
     $collation = $collation ?: $this->collation;
     foreach ($results as $row) {
@@ -195,7 +211,7 @@ class DrupalCharsetConverter {
       if (strstr($default, ':default')) {
         $params[':default'] = $row->Default;
       }
-      db_query($sql, $params);
+      $this->connection->query($sql, $params);
     }
   }
 }
