Replaced accum with an optimized version.

The new version keeps track of the tail of the accumulator and adds
to the end of that. This is faster than pushing the elements onto a
list and reversing it at the end.