Split main code into functions