Mechanical refactoring complete.

This is only the fisrt phase designed to extract some functionality out of the main namespace and into its own package. Subsequent commits will massage this code into a more desirable state.