Refactor to make code reusable