Python 3 compatibility refactoring (3.4+)