Updated the efficient gradient calculation functions