rewrite generator from scratch