fixed a perturbation bug and some gradient bugs too