Fix py3.x regressions

Hi @benjaoming, thanks for merging all those branches recently, but it caused regressions for py3.x. Here's the fix.