Port to python3

Port to python3, plus various minor improvements.