Refactoring to place table data in array index, modularisation of file writing function