Rearranged and cleaned code.