Refactored detect_mime_type method following Greg's code review