Improved login logic