Pulled main function logic into smaller functions