Refactored encoder