Fix a couple errors in gradient computation.