work in progress python 3 compatibility

Tests still don't run, but already fixed a whole bunch of stuff.