refactor: split functions into smaller units and improve structure
- decompose large functions into smaller focused helpers
- streamline control flow and improve maintainability
- reorganize code structure for better readability
- align implementation with modular design principles
- add unit test cases for updated functionality