Moved config code into its own module.
Cleaned up code. No significant logic changes.