Reused code from quicksort routine.