Python 3 Support and minor refactoring