Refactor out parts of the code