refactoring and Python 3 support