Improved authentication mechanism and error handling