Cleaned up code, added better logging, cleaned up possible errors during login/logout.