Refactored to improve modularity.