enhanced type detection and error handle