Implemented Newton's method