Add login hendler and refactor a little bit