Python 3 compatibility

Nothing fancy, just a few syntax fixes. Compatible with 2 and 3 with the same codebase.