cleanup and rewrite