Rearranged some code blocks