Bug fixes to sorted