added guard and fixed bug that would result in incompatible encodings