Some fixes in sorting code.