Replace -group-by with a straight-forward loop