factor out some common code