fixed a loop invariant bug