Refactor encoder
Requirement
Add comments and refactoring
Add specs