Fixed bug in sorting