moved functions around for easier comprehension and future editing