refactored logic for a more modular code structure.