Various fixes around sorting keys.