Added login error handler and cleaned up login procedure