Improved login query to accommodate different size tables