Refactored authentication code into separate file.