Lots of little fixes aimed at Python 3