Refactored code for better modularity.