rewrite toplevel loop