refactor code into functions