Refactored to support changes in Python 3. fixes #1