Revert "First pass of refactoring. Everything moved around for neater code."

This reverts commit 4bb975ebecf5e2a9c9e44278587b6e0860b4542f.