Should the inside of this loop recurse?  Alternative: factor the first half and the inner half into a shared routine.