Newton-Raphson method moved to a function, minor bug fix