- changed print to stdout.write()
 - added more exception handling