Add generic groupby function