Added file writing function