Fixes to sorting