More refactoring/cleanup

See commits for details