It would be an improvement if this could work with a generator. Consider changing this to ``while True``, and just ``break`` below on line 163.