Fixes calc_gavg by by rearranging for loop order