move common code in one place