Output data to a file
Refactor and add simple testing code