Refactoring some code to make it more reusable.