split main into functions