code cleanup
split code, move login op out