Most of the write method