refactor encoding to be able to support different encodings