some rewrite improvements