Refactoring sanitize function to handle dict and list cleaning