Lots of cleanup and refactoring, no logic changes