use sys.stdout.write instead of print to make it atomic