Fix string encoding/decoding to work in both python 2 and 3