Move code into more logical methods to clean up main.