refactoring to extract logic to methods instead of one long script