added saveToFile function