Changed code for Python 3 compatibility

Will have to check for Python 2 compatibility on OSX and Cygwin installations to be sure everything works before merging into master
additional fixes