Calculate sums along with filter. Huge speedup. Fixes #492.