Port most of the code to Python3 compatible.
Merge pull request #122 from wernight/master

Port most of the code to Python3 compatible.