Improve sum function