Refactor authentication stuff