Add timeout for while loop