Refactor authentication code & authentication tests