massive refactor, probably no longer works.
...