Important bugfix for numerical issues including unit test.