General sum function and implementations