added error handling for login function