Clean Up of Repeated Code

Some pieces of code are reused multiple times, tossing them in their own
function seemed feasible