refactored for readability and modularity