Replace string addition with writing directly to file