Weird behavior: sometimes passes the test, sometimes it loops indefinitely.