Rearranged some code and made some changes.