Partial rewrite and cleanup