improved encoding detection. fix #1.