Fix a couple of bugs in key-comparator

and add two tests that fail without the fixes.