Simplified the toCanonicalInput function