Build gradients outside of update function