pulled out parts of some functions into new functions to make original functions easier to read