Optimized regex and improved newline rules.

Previous commit had slow regex replacement, should be a lot faster now. Also improved the rules when it comes to inserting newlines and when it comes to restoring comments at the end of lines. Still very much untested at this point.