Refactoring and more work towards authentication