Fix bug #3289
Fix bug #3291, stack overflow in rewrite.