Factor out duplicated code.