Major refactor for more modular design.