diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
index 7098157e89d1ff3fa2d2c6a52fb4f27f9854c557..236cd18a849e3e5690bd601e7a7463948e8f2927 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosettingspage.cpp
@@ -137,7 +137,6 @@ private slots:
     void passwordEditingFinished();
     void keyFileEditingFinished();
     void testConfig();
-    void enableTestStop();
     void processSshOutput(const QString &data);
     void handleSshFinished();
     void stopConfigTest();
@@ -397,6 +396,7 @@ void MaemoSettingsWidget::testConfig()
     if (m_deviceTester)
         return;
 
+    m_ui->testConfigButton->disconnect();
     m_ui->testResultEdit->setPlainText(m_defaultTestOutput);
     QLatin1String sysInfoCmd("uname -rsm");
     QLatin1String qtInfoCmd("dpkg -l |grep libqt "
@@ -404,22 +404,15 @@ void MaemoSettingsWidget::testConfig()
         "|cut -d ' ' -f 2,3 |sed 's/~.*//g'");
     QString command(sysInfoCmd + " && " + qtInfoCmd);
     m_deviceTester = new MaemoSshRunner(currentConfig(), command);
-    connect(m_deviceTester, SIGNAL(connectionEstablished()),
-            this, SLOT(enableTestStop()));
     connect(m_deviceTester, SIGNAL(remoteOutput(QString)),
             this, SLOT(processSshOutput(QString)));
     connect(m_deviceTester, SIGNAL(finished()),
             this, SLOT(handleSshFinished()));
-    m_deviceTester->start();
-#endif
-}
-
-void MaemoSettingsWidget::enableTestStop()
-{
-    m_ui->testConfigButton->disconnect();
     m_ui->testConfigButton->setText(tr("Stop test"));
     connect(m_ui->testConfigButton, SIGNAL(clicked()),
             this, SLOT(stopConfigTest()));
+    m_deviceTester->start();
+#endif
 }
 
 void MaemoSettingsWidget::processSshOutput(const QString &data)
@@ -453,15 +446,14 @@ void MaemoSettingsWidget::stopConfigTest()
     qDebug("================> %s", Q_FUNC_INFO);
     if (m_deviceTester) {
         qDebug("Actually doing something");
-        m_deviceTester->disconnect();
+        m_ui->testConfigButton->disconnect();
         const bool buttonWasEnabled = m_ui->testConfigButton->isEnabled();
-        m_ui->testConfigButton->setEnabled(false);
+        m_deviceTester->disconnect();
         m_deviceTester->stop();
         delete m_deviceTester;
         m_deviceTester = 0;
         m_deviceTestOutput.clear();
         m_ui->testConfigButton->setText(tr("Test"));
-        m_ui->testConfigButton->disconnect();
         connect(m_ui->testConfigButton, SIGNAL(clicked()),
                 this, SLOT(testConfig()));
         m_ui->testConfigButton->setEnabled(buttonWasEnabled);
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
index 10f2bcfe3a63c5c05e3938d2de45440d3adc6e5e..a024a596895ed419ff615a5d9adb64520f2e1dfc 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.cpp
@@ -47,7 +47,7 @@ namespace Qt4ProjectManager {
 namespace Internal {
 
 MaemoSshThread::MaemoSshThread(const MaemoDeviceConfig &devConf)
-    : m_devConf(devConf)
+    : m_stopRequested(false), m_devConf(devConf)
 {
 }
 
@@ -60,7 +60,8 @@ MaemoSshThread::~MaemoSshThread()
 void MaemoSshThread::run()
 {
     try {
-        runInternal();
+        if (!m_stopRequested)
+            runInternal();
     } catch (const MaemoSshException &e) {
         m_error = e.error();
     }
@@ -68,13 +69,21 @@ void MaemoSshThread::run()
 
 void MaemoSshThread::stop()
 {
-    m_connection->stop();
+    m_mutex.lock();
+    m_stopRequested = true;
+    const bool hasConnection = !m_connection.isNull();
+    m_mutex.unlock();
+    if (hasConnection)
+        m_connection->stop();
 }
 
-void MaemoSshThread::setConnection(const MaemoSshConnection::Ptr &connection)
+template <class Conn> typename Conn::Ptr MaemoSshThread::createConnection()
 {
+    typename Conn::Ptr connection = Conn::create(m_devConf);
+    m_mutex.lock();
     m_connection = connection;
-    emit connectionEstablished();
+    m_mutex.unlock();
+    return connection;
 }
 
 MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf,
@@ -86,8 +95,9 @@ MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf,
 void MaemoSshRunner::runInternal()
 {
     MaemoInteractiveSshConnection::Ptr connection
-        = MaemoInteractiveSshConnection::create(m_devConf);
-    setConnection(connection);
+        = createConnection<MaemoInteractiveSshConnection>();
+    if (stopRequested())
+        return;
     connect(connection.data(), SIGNAL(remoteOutput(QString)),
             this, SIGNAL(remoteOutput(QString)));
     connection->runCommand(m_command);
@@ -102,8 +112,9 @@ MaemoSshDeployer::MaemoSshDeployer(const MaemoDeviceConfig &devConf,
 void MaemoSshDeployer::runInternal()
 {
     MaemoSftpConnection::Ptr connection
-        = MaemoSftpConnection::create(m_devConf);
-    setConnection(connection);
+        = createConnection<MaemoSftpConnection>();
+    if (stopRequested())
+        return;
     connect(connection.data(), SIGNAL(fileCopied(QString)),
             this, SIGNAL(fileCopied(QString)));
     connection->transferFiles(m_filePaths, m_targetDirs);
diff --git a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
index c29ec86461afd4e2e21ac034c9e868fd10f9603e..bb59514b58996afaaf6ea8b39e75b2fb030e26df 100644
--- a/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
+++ b/src/plugins/qt4projectmanager/qt-maemo/maemosshthread.h
@@ -45,6 +45,7 @@
 #include "maemodeviceconfigurations.h"
 #include "maemosshconnection.h"
 
+#include <QtCore/QMutex>
 #include <QtCore/QStringList>
 #include <QtCore/QThread>
 
@@ -64,19 +65,18 @@ public:
     virtual void run();
     ~MaemoSshThread();
 
-signals:
-    void connectionEstablished();
-
 protected:
     MaemoSshThread(const MaemoDeviceConfig &devConf);
-    virtual void runInternal()=0;
-    void setConnection(const MaemoSshConnection::Ptr &connection);
-    
-    const MaemoDeviceConfig m_devConf;
+    template <class Conn> typename Conn::Ptr createConnection();
+    bool stopRequested() const { return m_stopRequested; }
 
 private:
+    virtual void runInternal()=0;
 
+    bool m_stopRequested;
     QString m_error;
+    QMutex m_mutex;
+    const MaemoDeviceConfig m_devConf;
     MaemoSshConnection::Ptr m_connection;
 };