refactor for modularity and reuse