Rearranged and added code