Optimize chaos calculations and fix some bugs