Significantly simplified sorting algorithm