Refactoring the newton functions