Added some tests and test data for them.
Added some comments, fixed various potential future problem places as
revealed by testing.