Placed some of the code into functions