Refactor login/logout code