Porting to Python 3 and solving some problems