refactor login functionality