Fix python3 compatibility

This includes fixes related to the use of strings/bytes as well
dict.iteritems().