Python 3.x version with some improvements