WIP - cleaning up the hideous hack somewhat

Some debug output and comments cleaned up, some code reorganization, but
mostly just sorting out how to adequately test this. Still not sure

Uses bits from Nate.