optimization bugs fixed