Improve python 3 compatibility

...by explicitly encoding/decoding strings when writing/reading to files.