improved encoding handling