added file writing to the controller.