Could we move these subroutines of `encode()` after `encode()` for readability?