more robust type detection in face of nils value
added charset and BOM detection and handling