exception handling and print statements changed to Python 3-compatible