Fix bug in aggregation. The wrong index was used, resulting in
incorrect results.