Refactored code for modularity