Refactored to use array_reduce()