almost done with first-draft refactoring, completely untested and probably very broken.