Added functionality to write model to file