Added better error handling to preserveFormatting.