Debugging of rewrite.  Code now seems to run ok!