First cut at the code.

Undocumented.  That's on the to-fix list.  Also needs unit tests.