Correctly handle ]]>

Also remove some extraneous newlines.