restructure login logic