Generalized the rewriting system. State can now be passed to and from recursive rewrite calls.