Fixes to Sort code.