I'm not a fan of the while loop. After a short googling, I found this stack overflow answer which deals with the same problem: http://stackoverflow.com/a/1094933