Simplify encoding logic

Apologies. I should have (meant to) done this prior to submitting the first pull request.