Fix a couple of bugs in key-comparator

and add two tests that fail without the fixes.

See the tests added for some explanation of what I think the bugs are.  I'm happy to answer any questions about what I did, in case it looks wrong.  Also let me know if you prefer to receive pull requests on some branch besides master, e.g. develop.