Misc cleanup and Python 3 compatibility tweaks.