moved code into functions