use a buffer instead of writing directly to os.Stdout (thanks to tip from Robert Milton/Nigel Tao)