Fixed bug in sorting algorithm