             self.lockScreen()
 
             var counter = 0
-            var failure = false
-
-            for asset in assets {
-                guard failure == false else {
-                    break
+
+            let sftpSession = self.connectAndAuthenticate()
+
+            if (sftpSession != nil) {
+                for asset in assets {
+                    counter += 1
+
+                    // Allow since this always comes from PHAsset.fetchAssetsWithMediaType
+                    // swiftlint:disable:next force_cast
+                    let asset = asset as! PHAsset
+
+                    // This request must be synchronous otherwise the resultHandler ends up back on the main thread.
+                    let myOptions = PHImageRequestOptions()
+                    myOptions.synchronous = true
+
+                    PHImageManager.defaultManager().requestImageDataForAsset(asset, options: myOptions, resultHandler: {
+                        imageData, dataUTI, orientation, info in
+                        self.uploadPhoto(sftpSession!, imageData: imageData!, index: counter, totalNumber: assets.count, creationDate: asset.creationDate!)
+                    })
                 }
 
-                counter += 1
-
-                // Allow since this always comes from PHAsset.fetchAssetsWithMediaType
-                // swiftlint:disable:next force_cast
-                let asset = asset as! PHAsset
-
-                // This request must be synchronous otherwise the resultHandler ends up back on the main thread.
-                let myOptions = PHImageRequestOptions()
-                myOptions.synchronous = true
-
-                PHImageManager.defaultManager().requestImageDataForAsset(asset, options: myOptions, resultHandler: {
-                    imageData, dataUTI, orientation, info in
-                    do {
-                        try self.uploadPhoto(imageData!, index: counter, totalNumber: assets.count, creationDate: asset.creationDate!)
-                    } catch ConnectionError.NotAuthorized {
-                        self.updateStatus("Could not authorize - probably the username or password is wrong.")
-                        failure = true
-                    } catch ConnectionError.NotConnected {
-                        self.updateStatus("Could not connect - probably the hostname is wrong.")
-                        failure = true
-                    } catch {
-                        self.updateStatus("Unknown error")
-                        failure = true
-                    }
-                })
-            }
-
-            self.unlockScreen()
-
-            if !failure {
                 self.updateStatus("Uploading complete successfully.")
+
+                sftpSession!.disconnect()
+
+                self.unlockScreen()
 
                 let keychain = KeychainSwift()
 

        }
    }

    func uploadPhoto(imageData: NSData, index: Int, totalNumber: Int, creationDate: NSDate) throws {
        let host = self.host.text
        let port = self.port.text
        let username = self.username.text
        let password = self.password.text
        let session = NMSSHSession(host: host, port: Int(port!)!, andUsername: username)

        self.updateStatus("Connecting... ", count: index, total: totalNumber)
        session.connect()
        guard session.connected else {
            throw ConnectionError.NotConnected
        }

        self.updateStatus("Authenticating... ", count: index, total: totalNumber)
        session.authenticateByPassword(password)
        guard session.authorized else {
            throw ConnectionError.NotAuthorized
        }

        let sftpSession = NMSFTP.connectWithSession(session)
        let date = formatDate(creationDate)

        let finalFilePath =  self.remoteDir.text! + "/" + date + ".jpg"


        self.updateStatus("Uploading to temporary file...", count: index, total: totalNumber)
        sftpSession.writeContents(imageData, toFileAtPath: tempFilePath,
                                  progress: { sent in
                                    self.updateStatus("Uploading to temporary file...", count: index, total: totalNumber, percentage: Float(sent) / Float(length))
                                    return true
            }
        )
        self.updateStatus("Moving file to final location...", count: index, total: totalNumber)
        sftpSession.moveItemAtPath(tempFilePath, toPath: finalFilePath)
        self.updateStatus("Done.", count: index, total: totalNumber)
        NSLog(finalFilePath + " successfully written.")
    }

    func updateStatus(status: String, count: Int = 0, total: Int = 0, percentage: Float = -1) {
