Refactor average using reduce method