Added file writing logic and for loop.