Fix logic error in optimization introduced in last commit