refactor loggin