fixed another bug: sum was not set to zero when calculating mean several times.