Simplify code, plus make it run on python3

Untested :)