Fixed some runtime bugs. The optimization now works as expected.