Refactor login logic