Working version with CR's removed.
Lots of tiny refactoring/renames/comment changes maybe it'll even work.