python2/3 portability and bug fix