Rearranged code & removed some unused code