Broken up the main function to make the code more readable