Improved authentication logic.