 		app.get '/ssids', (req, res) ->
 			res.send(ssidList)
 		app.post '/connect', (req, res) ->
-			if req.body.ssid and req.body.passphrase
-				console.log('Selected ' + req.body.ssid)
-				res.send('OK')
-				server.close()
-				iptables.delete { table: 'nat', rule: 'PREROUTING -i tether -j TETHER'}, ->
-					iptables.flush 'nat', 'TETHER', ->
-						dnsServer.kill()
-						console.log('Server closed and captive portal disabled')
-						wifi.joinWithAgentAsync(req.body.ssid, req.body.passphrase)
-						.then ->
-							console.log('Joined! Exiting.')
-							retryCallback()
-						.catch (err) ->
-							console.log(err)
-							return startServer(wifi)
+			if not (req.body.ssid? and req.body.passphrase?)
+				return res.sendStatus(400)
+			console.log('Selected ' + req.body.ssid)
+			res.send('OK')
+			server.close()
+			iptables.deleteAsync({ table: 'nat', rule: 'PREROUTING -i tether -j TETHER'})
+			.catch(ignore)
+			.then ->
+				iptables.flushAsync('nat', 'TETHER')
+			.catch(ignore)
+			.then ->
+				dnsServer.kill()
+				console.log('Server closed and captive portal disabled')
+				wifi.joinWithAgentAsync(req.body.ssid, req.body.passphrase)
+			.then ->
+				console.log('Joined! Exiting.')
+				retryCallback()
+			.catch (err) ->
+				console.log('Error joining network', err, err.stack)
+				return startServer(wifi)
+
 		app.use (req, res) ->
 			res.redirect('/')
 
 		# Create TETHER iptables chain (will silently fail if it already exists)
 		iptables.createChainAsync('nat', 'TETHER')
-		.catch ->
-			return
+		.catch(ignore)
 		.then ->
 			iptables.deleteAsync({ table: 'nat', rule: 'PREROUTING -i tether -j TETHER'})
-		.catch ->
-			return
+		.catch(ignore)
 		.then ->
 			iptables.flushAsync('nat', 'TETHER')
 		.then ->

	console.log('Getting networks list')
	wifi.getNetworksAsync().then (list) ->
		ssidList = list
	.then ->
		wifi.openHotspotAsync(ssid, passphrase)
	.then ->
		console.log('Hotspot enabled')
		dnsServer = spawn('named', ['-f'])
		getIptablesRules (err, iptablesRules) ->
			iptables.appendManyAsync(iptablesRules)
			.then ->
				console.log('Captive portal enabled')
				server = app.listen port, ->
					console.log('Server listening')

console.log('Starting node connman app')

ssidList = null
dnsServer = null

ignore = ->

getIptablesRules = (callback) ->
	async.retry {times: 100, interval: 100}, (cb) ->
		Promise.try ->
			os.networkInterfaces().tether[0].address
		.nodeify(cb)
	, (err, myIP) ->
		return callback(err) if err?
		callback null, [
				table: 'nat'
				rule: 'PREROUTING -i tether -j TETHER'
