reorganized code into functions