refactored to ensure compatibility with python 3.4