factor out common stuff