encoding logic