Fixed bug in rewrite that caused deadlock