encoding + small refactoring