Tweaks of sum functions

Some internal refactoring. To make handling of summing over empty array or singleton arrays consistent (and more efficient for when the elements are commonly used types).