major refactor of authentication code