Refactor into a smaller function.