Added some code to handle unsuccessful registration and login, and to
stop the network layer threads properly if it comes to that.