Improved login function.