refactored for clarity, modularity