Make sure the loop terminates (ugly coding)