logic rewrite