Refactoring code and Saving all the data into a file