Refactor login code