Added gradients calculation at the end of script. Still not tested.