moved code into functions to improve floww