added some code for Write mode