Replace the while with a for