Moved rewrites stuff to a helper