Refactored login and registration code