implement groupby, cleaned implementation of make-n-groups