added login and logout, and some refactoring