First version of the rewrite and clean up.