refactored the end of login(), phew..