Improved tests, fixed nans and other small changes.