Transfer generator into recursive function