Rearranged some blocks and various cleanup