factored out bits of code into own functions