Base code for write.