rearranged and added some code for dumping stages