Refactoring and multiple small fixes.
Bugs fixed in invnewton example. Results may still be incorrect.