Fixing bug with multiple aggregations

(needs cleaning up)