Starting rewrite

Working version of rewrite