modified write to work appropriately