explaining complex while loop in comment