abstracted some code out into functions