More refactoring to improve modularity and allow for easier dependency injection