modified write function