Enhance efficiency and fault tolerance of type conversion

Use str type as much as possible on Py2 instead of unicode
Convert from bytes to unicode with errors=replace on Py3 to prevent unicode encoding if some bad data slips in