Use sys.stdout.flush() to make output order correct