Almost total rewrite