Fixed waitForFile iteration logic