Moving code around, minorr fixes
Python 2/3 compatibility