More refactoring to make the main method smaller.