port to Python3, and compatible with Python2