Jeremy’s version with Python 3 port and various other changes gathered together