refactor encoding to use util functions