refactored function for login