reorganize code into functions