Add Python 3 support

Credits to @edschofield for the work.
Looks like an unresolved merge conflict...