rewrite algorithms