Split main func into smaller funcs