Fix loop ending