improved encoding error handlding