Encode output when necessary. Fixes #30