Rewrite code logic