Refactor authentication