More implementation of gradient calculations.