pull extract code into functions and clean up main