Refactoring logging and gradients