Error and Rewriting handling fixes