refactor code for python3

May as well switch this now; could have saved time by doing it from the
start!