Added two dimensional averaging function