Fixing the optimize bug