Fixes to sorting

Fixes to sorting