Implemented newton method