some more major refactoring, but work still to be done