Fix off-by-correct bug in optimize