refactor loops in partitiondriver

Mostly to dry out the double loops with preclustering