Refactor to use dependency injection