python3 and python2 compatibility, fix some minor bugs