Moved relevant fns to rewrite.go