Added ability to logout.
Tidied and simplified code.