Refactor authentication code.
Re-order some things.