Refactor registration and login function