rearranged functions to be invoked in a more logical order