Sweeping refactoring

Quite a large set of changes. Made a number of changes in the interest of reducing code smell and fixing various TODOs.