Added code to check the gradient.