Modified to automatically identify what encoding is used.