Added averaging algorithm.