Refactor a bit to de-duplicate code.