Better login system and some code refactoring