Add aggr_by function and some minor changes