Make collapseWhitespace more resilient