Speed fixes 2

This removes slow loops in the aggregation functions and should make the site faster.

cc @smit1678 @jueyang