make write() safe