rearranged functions and added comments