some cleanup and preparation for nicer tests
bunch of refactoring and tests