weighted average refactor