Fix loop termination, test pass