Refactored code to avoid a deprecation warning in Python 2.7