Rewrite logic