Fixes from @deepankgupta -- thanks!
Added tests by @deepankgupta.
Cleanup unions used to implement optional fields.