Fix a bug in Output.Write, and improve testing coverage of error cases.