Moved gradients to separate functions