Moved authentication to a function 'authenticate' in its own file