factorize common code