Fixes to close and write.