Refactor the aggregation mechanism to avoid relying on side effects

See https://bugs.launchpad.net/openquake/+bug/1096880