Refactoring of average functionality. Add optional precision argument.