Modified to be compatible with Python 2 and Python 3.