new encoding conversion code (better structure)