Simplified sorting code.