Refactor encoder state