Lots of code cleanup.  Remove dead code, simplify API.