use reduce
refactored to use reduce