refactored sum of an array