Adapted to Python 3