Speed improvements

The sum function was taking the most time in the profile.  Replacing it with explicit sums saves a bunch of time.