Clean-up code for clarity / make binary-safe

This closes #15, and organizes the file so that it's easier to follow.

I was going to maintain this as a separate fork, since it's a major structure change, but am willing to merge it back upstream and abandon the fork if this PR is approved.