cleaned up for version with four independent gradient calculations, which are then averaged.