Fix aggregation and sort-by