Python 3 compatibility refactoring.