Fix a couple of bugs in key-comparator

and add two tests that fail without the fixes.
Merge commit '6d1a' into develop