Added more tests for authentication and refactored some code