Refactoring for better dependency injection