Fix loop condition check order