Refactored login method to be more reusable