Updated final averaging function