Improved encoding detection