improved aggregation logic