Use .end rather than .write.close, so that error handling stays working