The Great Refactoring (#528)

Lots of logic separation and code cleanup.