rearranged and added comments