added loop to write table