modified code to python3 compatible