Major refactoring of regression function