Major refactor to enable support for vairous authentication systems.