Refactored encoder.