Better simple partitioning algorithm