fixed a bug regarding the gradient calculation.