make changes for python3 compatibility
refactor output handling for python3 compatibility