Split up main function into smaller functions. Did not compile so might not work.