rearranged functions and commented