various calculation corrections

Hi, I really enjoyed playing with your project. There were some small inaccuracies in the optimisation parts. After introducing these changes it should improve performance.