Fix handling of incorrect encoding