Fix loop condition bug