factoring out common code