Fixed many encoding-related bugs

The most important lesson to take away from this is:

 1. Always use the `unicode_literals` future import
 2. Always decode input
 3. Always use unicode internally
 4. Always encode output

There should never be a `print(value)` statement in this code without an
`.encode(DEFAULT_ENCODING)` call, unless you're sure that the value is
already a bytestring or that the value safely converts to ASCII.