Improved encoder.
Refactoring.
Fixed merge conflict.