Improved encoding handling