Make the gradient function better