Added dependency injection and refactored appropriately