Fix speed issue in Python 2