Fix bug with wrong gradients