Fixed bug in gradient computation