Added logout functionality and cleaned up code a little.