aggregation: refactor aggregation process, move aggregation outside the main function