I got that code from here http://stackoverflow.com/a/7933931/832065 . I usually don't just copy code without not understanding it and here I also don't really know why there is a do while loop. It seems to be the right thing though.