Corrected the gradient problem. Need to clean the code.