Simplify encoding logic

Apologies. I should have (meant to) done this prior to submitting pull request #258.