rewrite loop