Abstracted some repeated code into functions