Python3 integration

Include #32, #33 and a range of fixes. All tests now running and pass on Python 3. (cc @regebro )