Improve closing of output buffer