refactor dictionary encoding