bug fixed in computing the gradient