Modularizing the write tsv function. Added some comments around the code for better readability.