Work fleshing out the functionality.

Compiles, not tested.
fixed obvious functional issues, passes basic round trip tests
minor re-factoring and documentation clean-up