Initial refactoring for better gradient control.