refactor out reduce functions