Fixed the rewrite code to preserve formatting and newlines from the original text.