Miscellaneous changes

no functional changes, only some refactoring, all tests pass