Rewrite and cleanup