rewrite using generator