Changes for Python3

Inspired in large part by https://wiki.python.org/moin/PortingExtensionModulesToPy3k