refactor and finish login logic