- some refactoring to make the main function more readable