Refactor some common bits out.