Refactored to make code more reusable.