refactoring how the main part of the script works.