Fix various Python 3 compatibility issues.