Optimization bugs fixed