Refactored code for increased modularity