diff --git a/commands/core/core.drush.inc b/commands/core/core.drush.inc
index e03bd5c..4a78a6d 100644
--- a/commands/core/core.drush.inc
+++ b/commands/core/core.drush.inc
@@ -1017,7 +1017,7 @@ function drush_core_execute() {
     $site = drush_sitealias_get_record($alias);
     if (!empty($site['remote-host'])) {
       // Remote, so execute an ssh command with a bash fragment at the end.
-      $exec = drush_shell_proc_build($site, $cmd);
+      $exec = drush_shell_proc_build($site, $cmd, TRUE);
       return drush_shell_proc_open($exec);
     }
   }
diff --git a/includes/backend.inc b/includes/backend.inc
index 943c243..a2c6df6 100644
--- a/includes/backend.inc
+++ b/includes/backend.inc
@@ -768,19 +768,22 @@ function _drush_backend_generate_command($site_record, $command, $args = array()
       unset($command_options[$key]);
     }
   }
+  
+  $cmd[] = $command;
   foreach ($args as $arg) {
-    $command .= ' ' . drush_escapeshellarg($arg, $os);
+    $cmd[] = drush_escapeshellarg($arg, $os);
   }
   $option_str = _drush_backend_argument_string($command_options, $os);
   if (!empty($option_str)) {
-    $command .= " " . $option_str;
+    $cmd[] = " " . $option_str;
   }
   if (!empty($backend_options['backend']) && empty($backend_options['interactive'])) {
-    $command .= " --backend=2";
+    $cmd[] = "--backend=2";
   }
   if (!empty($backend_options['interactive'])) {
-    $command .= ' --invoke';
+    $cmd[] .= '--invoke';
   }
+  $command = implode(' ', array_filter($cmd, 'strlen'));
   if (!is_null($hostname)) {
     if (drush_is_windows($os)) {
       if (!is_null($username) && (array_key_exists('winrs-password', $site_record))) {
@@ -792,7 +795,14 @@ function _drush_backend_generate_command($site_record, $command, $args = array()
       $username = (!is_null($username)) ? drush_escapeshellarg($username, "LOCAL") . "@" : '';
       $ssh_options = $site_record['ssh-options'];
       $ssh_options = (!is_null($ssh_options)) ? $ssh_options : drush_get_option('ssh-options', "-o PasswordAuthentication=no");
-      $command = "ssh " . $ssh_options . " " . $username . drush_escapeshellarg($hostname, "LOCAL") . " " . drush_escapeshellarg($command . ' 2>&1', "LOCAL");
+
+      $ssh_cmd[] = "ssh";
+      $ssh_cmd[] = $ssh_options;
+      $ssh_cmd[] = $username . drush_escapeshellarg($hostname, "LOCAL");
+      $ssh_cmd[] = drush_escapeshellarg($command . ' 2>&1', "LOCAL");
+
+      // Remove NULLs and separate with spaces
+      $command = implode(' ', array_filter($ssh_cmd, 'strlen'));
     }
   }
   else {
diff --git a/includes/drush.inc b/includes/drush.inc
index 9a75298..f5a0b5b 100644
--- a/includes/drush.inc
+++ b/includes/drush.inc
@@ -939,13 +939,13 @@ function drush_remote_command() {
   $remote_host = drush_get_option('remote-host');
   // Get the command early so that we can allow commands to directly handle remote aliases if they wish
   $command = drush_parse_command();
-  if (isset($command) && array_key_exists('mask-local-cli-options', $command)) {
+  if (is_array($command) && array_key_exists('mask-local-cli-options', $command)) {
     $global_cli_options = drush_get_context('DRUSH_GLOBAL_CLI_OPTIONS', array());
     drush_expand_short_form_options($global_cli_options);
     drush_set_context('cli', $global_cli_options);
     _drush_bootstrap_global_options();
   }
-  if (isset($command) && array_key_exists('handle-remote-commands', $command)) {
+  if (is_array($command) && array_key_exists('handle-remote-commands', $command)) {
     return FALSE;
   }
   if (isset($remote_host)) {
diff --git a/includes/exec.inc b/includes/exec.inc
index 9888289..03db788 100644
--- a/includes/exec.inc
+++ b/includes/exec.inc
@@ -156,7 +156,7 @@ function _drush_shell_exec($args, $interactive = FALSE) {
  * @return string
  *   A string suitable for execution with drush_shell_remote_exec().
  */
-function drush_shell_proc_build($site, $command = NULL, $cd = NULL) {
+function drush_shell_proc_build($site, $command = '', $cd = NULL) {
   // Build up the command. TODO: We maybe refactor this soon.
   $hostname = drush_escapeshellarg($site['remote-host'], "LOCAL");
   $username = isset($site['remote-user']) ? drush_escapeshellarg($site['remote-user'], "LOCAL") . "@" : '';
@@ -166,19 +166,29 @@ function drush_shell_proc_build($site, $command = NULL, $cd = NULL) {
   }
 
   $cmd = "ssh " . $ssh_options . " " . $username . $hostname;
+
+  if ($cd === TRUE) {
+    if (array_key_exists('root', $site)) {
+      $cd = $site['root'];
+    }
+    else {
+      $cd = FALSE;
+    }
+  }
+  if ($cd) {
+    $command = 'cd ' . drush_escapeshellarg($cd, $site['os']) . ' && ' . $command;
+  }
+
   if (!empty($command)) {
     if (!drush_get_option('escaped', FALSE)) {
-      $cmd .= " " . drush_escapeshellarg($command);
+      $cmd .= " " . drush_escapeshellarg($command, $site['os']);
     }
     else {
       $cmd .= " $command";
     }
   }
-
-  if ($cd) {
-    $cd = 'cd ' . drush_escapeshellarg($cd, $site['os']) . ' && ';
-  }
-  return $cd . $cmd;
+  
+  return $cmd;
 }
 
 /*
diff --git a/tests/drush_testcase.inc b/tests/drush_testcase.inc
index 0c463e8..4e5559a 100644
--- a/tests/drush_testcase.inc
+++ b/tests/drush_testcase.inc
@@ -33,7 +33,7 @@ abstract class Drush_TestCase extends PHPUnit_Framework_TestCase {
    */
   public static function tearDownAfterClass() {
     if (file_exists(UNISH_SANDBOX)) {
-      unish_file_delete_recursive(UNISH_SANDBOX);
+      //unish_file_delete_recursive(UNISH_SANDBOX);
     }
   }
 
@@ -235,6 +235,7 @@ abstract class Drush_CommandTestCase extends Drush_TestCase {
     $this->_output = FALSE;
     $this->log("Executing: $command", 'notice');
     exec($command, $this->_output, $return);
+    //$this->_output = array_filter(array_map('trim', $this->_output), 'strlen');;
     $this->assertEquals($expected_return, $return, 'Unexpected exit code: ' .  $command);
     return $return;
   }
@@ -258,7 +259,7 @@ abstract class Drush_CommandTestCase extends Drush_TestCase {
     *   An exit code.
     */
   function drush($command, array $args = array(), array $options = array(), $site_specification = NULL, $cd = NULL, $expected_return = self::EXIT_SUCCESS) {
-    $global_option_list = array('simulate', 'root', 'uri', 'include', 'config');
+    $global_option_list = array('simulate', 'root', 'uri', 'include', 'config', 'ssh-options');
     // insert "cd ... ; drush"
     $cmd[] = $cd ? sprintf('cd %s;', self::escapeshellarg($cd)) : NULL;
     $cmd[] = UNISH_DRUSH;
diff --git a/tests/shellAliasTest.php b/tests/shellAliasTest.php
index 8180250..31489db 100644
--- a/tests/shellAliasTest.php
+++ b/tests/shellAliasTest.php
@@ -19,7 +19,7 @@ class shellAliasesCase extends Drush_CommandTestCase {
         'pull' => '!git pull',
       );
     ";
-    file_put_contents(UNISH_SANDBOX . '/drushrc.php', $contents);
+    file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
   }
 
   /*
@@ -29,7 +29,7 @@ class shellAliasesCase extends Drush_CommandTestCase {
     $options = array(
       'config' => UNISH_SANDBOX,
     );
-    $this->drush('glopts', array('php_configuration'), $options);
+    $this->drush('glopts', array(), $options);
     $output = $this->getOutput();
     $this->assertContains('These options are applicable to most drush commands.', $output, 'Successfully executed local shell alias to drush command');
   }
@@ -53,10 +53,11 @@ class shellAliasesCase extends Drush_CommandTestCase {
     $options = array(
       'config' => UNISH_SANDBOX,
       'simulate' => NULL,
+      'ssh-options' => '',
     );
-    $this->drush('glopts', array('php_configuration'), $options, 'user@server/path/to/drupal#sitename');
+    $this->drush('glopts', array(), $options, 'user@server/path/to/drupal#sitename');
     // $expected might be different on non unix platforms. We shall see.
-    $expected = "Simulating backend invoke: ssh user@server 'drush --simulate --uri=sitename --root=/path/to/drupal topic core-global-options --invoke 2>&1' 2>&1";
+    $expected = "Simulating backend invoke: ssh user@server 'drush  --simulate --nocolor --uri=sitename --root=/path/to/drupal --config=/tmp/drush-sandbox topic core-global-options --invoke 2>&1' 2>&1";
     $output = $this->getOutput();
     $this->assertEquals($expected, $output, 'Expected remote shell alias to a drush command was built');
   }
@@ -65,11 +66,12 @@ class shellAliasesCase extends Drush_CommandTestCase {
     $options = array(
       'config' => UNISH_SANDBOX,
       'simulate' => NULL,
+      'ssh-options' => '',
       'rebase' => NULL,
     );
     $this->drush('pull', array('origin'), $options, 'user@server/path/to/drupal#sitename');
     // $expected might be different on non unix platforms. We shall see.
-    $expected = "Calling proc_open(ssh user@server \"cd '/path/to/drupal' && git pull origin --rebase\");";
+    $expected = "Calling proc_open(ssh  user@server 'cd /path/to/drupal && git pull origin --rebase');";
     $output = $this->getOutput();
     $this->assertEquals($expected, $output, 'Expected remote shell alias to a bash command was built');
   }
diff --git a/tests/siteSshTest.php b/tests/siteSshTest.php
index 5ae05c0..90ac079 100644
--- a/tests/siteSshTest.php
+++ b/tests/siteSshTest.php
@@ -22,7 +22,7 @@ class siteSshCase extends Drush_CommandTestCase {
     );
     $this->drush('ssh', array('@interactive', ), $options);
     $output = $this->getOutput();
-    $expected = sprintf('ssh -o PasswordAuthentication=no %s@%s', self::escapeshellarg('user123'), self::escapeshellarg('my.server.com'));
+    $expected = sprintf('Calling proc_open(ssh -o PasswordAuthentication=no %s@%s);', self::escapeshellarg('user123'), self::escapeshellarg('my.server.com'));
     $this->assertEquals($expected, $output);
   }
 
@@ -42,7 +42,7 @@ class siteSshCase extends Drush_CommandTestCase {
     );
     $this->drush('ssh', array('@non-interactive', 'date'), $options);
     $output = $this->getOutput();
-    $expected = sprintf('ssh -o PasswordAuthentication=no %s@%s %s', self::escapeshellarg('user123'), self::escapeshellarg('my.server.com'), self::escapeshellarg('date'));
+    $expected = sprintf('Calling proc_open(ssh -o PasswordAuthentication=no %s@%s %s);', self::escapeshellarg('user123'), self::escapeshellarg('my.server.com'), self::escapeshellarg('date'));
     $this->assertEquals($expected, $output);
   }
 
