Finite diff gradients fixed and unified, test_gradient updated. Cached object g generalized a bit.