Corrected version of rewrite code