Updated writeFile method