Refactored the single parts of the program into separate functions