Refactor binary encoding