Port to Python 3: use next(generator) instead of generator.next()