Fixed the loop so the rows and cols are written correctly