Move python 2/3 compatibility code to new module.
Some reorganization of the input code.