rearranged functions to make order of operations clearer