Restructured writeFiles() to use for loop and list.