Refactor encoder into a function and eliminate the class