Refactors some common code.