fixed some indexing issues

Ordinary averaging now does not produce different results than "tree" merging, except for very small differences that can be explained by floating point numerical issues.

The change required additional range checks for indices and a starting value of 1 for i.