A bunch of rewriting and cleanup of the sanitization code.