Safer code for input format auto-detection