aggregator: refactor flushing logic