More formatting cleanup and some minor refactoring