Split main into functions