First attempt at partitioning algorithm