Refactoring how gradients are taken