Index: library/bdm/base/bdmbase.h
===================================================================
--- library/bdm/base/bdmbase.h (revision 504)
+++ library/bdm/base/bdmbase.h (revision 507)
@@ -449,5 +449,5 @@
 		//! @{
 
-		RV _rv() {
+		RV _rv() const {
 			return ep->_rv();
 		}
@@ -742,32 +742,6 @@
 UIREGISTER (mepdf);
 
-//!\brief Chain rule of pdfs - abstract part common for mprod and merger.
-//!this abstract class is common to epdf and mpdf
-//!\todo Think of better design - global functions rv=get_rv(Array<mpdf*> mpdfs); ??
-class compositepdf
-{
-	protected:
-		//! Elements of composition
-		Array<mpdf*> mpdfs;
-		bool owning_mpdfs;
-	public:
-		compositepdf() : mpdfs (0) {};
-		compositepdf (Array<mpdf*> A0, bool own = false) {
-			set_elements (A0, own);
-		};
-		void set_elements (Array<mpdf*> A0, bool own = false) {
-			mpdfs = A0;
-			owning_mpdfs = own;
-		};
-		//! find common rv, flag \param checkoverlap modifies whether overlaps are acceptable
-		RV getrv (bool checkoverlap = false);
-		//! common rvc of all mpdfs is written to rvc
-		void setrvc (const RV &rv, RV &rvc);
-		~compositepdf() {
-			if (owning_mpdfs) for (int i = 0; i < mpdfs.length(); i++) {
-					delete mpdfs (i);
-				}
-		};
-};
+//! \brief Combines RVs from a list of mpdfs to a single one.
+RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, bool checkoverlap = false );
 
 /*! \brief Abstract class for discrete-time sources of data.
Index: library/bdm/base/bdmbase.cpp
===================================================================
--- library/bdm/base/bdmbase.cpp (revision 504)
+++ library/bdm/base/bdmbase.cpp (revision 507)
@@ -306,5 +306,6 @@
 }
 
-RV compositepdf::getrv ( bool checkoverlap ) {
+RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs,
+		      bool checkoverlap ) {
 	RV rv; //empty rv
 	bool rvaddok;
@@ -312,14 +313,8 @@
 		rvaddok = rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
 		// If rvaddok==false, mpdfs overlap => assert error.
-		it_assert_debug ( rvaddok || ( !checkoverlap ), "mprod::mprod() input mpdfs overlap in rv!" );
-	};
+		it_assert_debug ( rvaddok || !checkoverlap, "mprod::mprod() input mpdfs overlap in rv!" );
+	}
+
 	return rv;
-}
-
-void compositepdf::setrvc ( const RV &rv, RV &rvc ) {
-	for ( int i = 0; i < mpdfs.length(); i++ ) {
-		RV rvx = mpdfs ( i )->_rvc().subt ( rv );
-		rvc.add ( rvx ); //add rv to common rvc
-	};
 }
 
Index: library/bdm/stat/merger.cpp
===================================================================
--- library/bdm/stat/merger.cpp (revision 504)
+++ library/bdm/stat/merger.cpp (revision 507)
@@ -5,8 +5,7 @@
 namespace bdm {
 
-merger_base::merger_base ( const Array<mpdf*> &S, bool own ) {
-	DBG = false;
-	dbg_file = NULL;
-	set_sources ( S, own );
+merger_base::merger_base ( const Array<shared_ptr<mpdf> > &S ):
+	Npoints(0), DBG(false), dbg_file(0) {
+	set_sources ( S );
 }
 
Index: library/bdm/stat/emix.cpp
===================================================================
--- library/bdm/stat/emix.cpp (revision 504)
+++ library/bdm/stat/emix.cpp (revision 507)
@@ -202,4 +202,25 @@
 	return aprgiw;
 };
+
+void mprod::set_elements (const Array<shared_ptr<mpdf> > &mFacs ) {
+	mpdfs = mFacs;
+	dls.set_size ( mFacs.length() );
+
+	set_ep ( iepdf);
+	RV rv = get_composite_rv ( mpdfs, true );
+	set_rv ( rv );
+	iepdf.set_parameters ( rv._dsize() );
+
+	for ( int i = 0; i < mpdfs.length(); i++ ) {
+		RV rvx = mpdfs ( i )->_rvc().subt ( rv );
+		rvc.add ( rvx ); // add rv to common rvc
+	}
+
+	// rv and rvc established = > we can link them with mpdfs
+	for ( int i = 0; i < mpdfs.length(); i++ ) {
+		dls ( i ) = new datalink_m2m;
+		dls ( i )->set_connection ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() );
+	}
+}
 
 vec mmix::samplecond(const vec &cond) {
Index: library/bdm/stat/merger.h
===================================================================
--- library/bdm/stat/merger.h (revision 488)
+++ library/bdm/stat/merger.h (revision 507)
@@ -42,14 +42,21 @@
 */
 
-class merger_base : public compositepdf, public epdf {
+class merger_base : public epdf {
 protected:
+	//! Elements of composition
+	Array<shared_ptr<mpdf> > mpdfs;
+
 	//! Data link for each mpdf in mpdfs
 	Array<datalink_m2e*> dls;
+
 	//! Array of rvs that are not modelled by mpdfs at all, \f$ z_i \f$
 	Array<RV> rvzs;
+
 	//! Data Links for extension \f$ f(z_i|x_i,y_i) \f$
 	Array<datalink_m2e*> zdls;
+
 	//! number of support points
 	int Npoints;
+
 	//! number of sources
 	int Nsources;
@@ -77,16 +84,14 @@
 	//! @{
 
-	//!Empty constructor
-	merger_base () : compositepdf() {
-		DBG = false;
-		dbg_file = NULL;
+	//! Default constructor
+	merger_base () : Npoints(0), Nsources(0), DBG(false), dbg_file(0) {
 	}
 
 	//!Constructor from sources
-	merger_base ( const Array<mpdf*> &S, bool own = false );
+	merger_base ( const Array<shared_ptr<mpdf> > &S );
 
 	//! Function setting the main internal structures
-	void set_sources ( const Array<mpdf*> &Sources, bool own ) {
-		compositepdf::set_elements ( Sources, own );
+	void set_sources ( const Array<shared_ptr<mpdf> > &Sources ) {
+		mpdfs = Sources;
 		Nsources = mpdfs.length();
 		//set sizes
@@ -95,7 +100,13 @@
 		zdls.set_size ( Sources.length() );
 
-		rv = getrv ( /* checkoverlap = */ false );
+		rv = get_composite_rv ( mpdfs, /* checkoverlap = */ false );
+
 		RV rvc;
-		setrvc ( rv, rvc );  // Extend rv by rvc!
+		// Extend rv by rvc!
+		for ( int i = 0; i < mpdfs.length(); i++ ) {
+			RV rvx = mpdfs ( i )->_rvc().subt ( rv );
+			rvc.add ( rvx ); // add rv to common rvc
+		}
+
 		// join rv and rvc - see descriprion
 		rv.add ( rvc );
@@ -325,13 +336,17 @@
 	//!\name Constructors
 	//!@{
-	merger_mix () {};
-	merger_mix ( const Array<mpdf*> &S, bool own = false ) {
-		set_sources ( S, own );
-	};
+	merger_mix ():Ncoms(0), effss_coef(0), stop_niter(0) { }
+
+	merger_mix ( const Array<shared_ptr<mpdf> > &S ):
+		Ncoms(0), effss_coef(0), stop_niter(0) {
+		set_sources ( S );
+	}
+
 	//! Set sources and prepare all internal structures
-	void set_sources ( const Array<mpdf*> &S, bool own ) {
-		merger_base::set_sources ( S, own );
+	void set_sources ( const Array<shared_ptr<mpdf> > &S ) {
+		merger_base::set_sources ( S );
 		Nsources = S.length();
 	}
+
 	//! Set internal parameters used in approximation
 	void set_parameters ( int Ncoms0 = DFLT_Ncoms, double effss_coef0 = DFLT_effss_coef ) {
Index: library/bdm/stat/emix.h
===================================================================
--- library/bdm/stat/emix.h (revision 504)
+++ library/bdm/stat/emix.h (revision 507)
@@ -270,5 +270,8 @@
 Note that
 */
-class mprod: public compositepdf, public mpdf {
+class mprod: public mpdf {
+private:
+	Array<shared_ptr<mpdf> > mpdfs;
+
 protected:
 	//! Data link for each mpdfs
@@ -279,29 +282,14 @@
 
 public:
-	/*!\brief Constructor from list of mFacs,
+	//! \brief Default constructor
+	mprod() { }
+
+	/*!\brief Constructor from list of mFacs
 	*/
-	mprod() : iepdf( ) { }
-	mprod ( Array<mpdf*> mFacs ) :
-			iepdf ( ) {
+	mprod ( const Array<shared_ptr<mpdf> > &mFacs ) {
 		set_elements ( mFacs );
 	}
 
-	void set_elements ( Array<mpdf*> mFacs , bool own = false ) {
-
-		compositepdf::set_elements ( mFacs, own );
-		dls.set_size ( mFacs.length() );
-
-		set_ep ( iepdf);
-		RV rv = getrv ( true );
-		set_rv ( rv );
-		iepdf.set_parameters ( rv._dsize() );
-		setrvc (_rv(), rvc );
-		// rv and rvc established = > we can link them with mpdfs
-		for ( int i = 0; i < mpdfs.length(); i++ ) {
-			dls ( i ) = new datalink_m2m;
-			dls ( i )->set_connection ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() );
-		}
-
-	};
+	void set_elements (const Array<shared_ptr<mpdf> > &mFacs );
 
 	double evallogcond ( const vec &val, const vec &cond ) {
@@ -359,5 +347,4 @@
 	}
 
-	~mprod() {};
 	//! Load from structure with elements:
 	//!  \code
@@ -368,7 +355,13 @@
 	//!@}
 	void from_setting ( const Setting &set ) {
-		Array<mpdf*> Atmp; //temporary Array
-		UI::get ( Atmp, set, "mpdfs", UI::compulsory );
-		set_elements ( Atmp, true );
+		Array<mpdf*> atmp; //temporary Array
+		UI::get ( atmp, set, "mpdfs", UI::compulsory );
+		
+		Array<shared_ptr<mpdf> > btmp ( atmp.length() );
+		for (int i = 0; i < atmp.length(); ++i) {
+			btmp ( i ) = shared_ptr<mpdf> ( atmp ( i ) );
+		}
+
+		set_elements ( btmp );
 	}
 
Index: library/tests/merger_2d_test.cpp
===================================================================
--- library/tests/merger_2d_test.cpp (revision 504)
+++ library/tests/merger_2d_test.cpp (revision 507)
@@ -21,19 +21,19 @@
 	xy.add ( y );
 
-	enorm<fsqmat> f1;
-	f1.set_rv ( xy );
-	enorm<fsqmat> f2;
-	f1.set_rv ( xy );
+	shared_ptr<enorm<fsqmat> > f1 = new enorm<fsqmat>();
+	f1->set_rv ( xy );
+	shared_ptr<enorm<fsqmat> > f2 = new enorm<fsqmat>();
+	f2->set_rv ( xy );
 
 	mat R1 ( "0.5 0.48; 0.48 0.5" );
 	mat R2 ( "0.5 0; 0 0.1" );
-	f1.set_parameters ( "1 1", R1 );
-	f2.set_parameters ( "1 1", mat ( "0.5 0; 0 0.1" ) );
+	f1->set_parameters ( "1 1", R1 );
+	f2->set_parameters ( "1 1", mat ( "0.5 0; 0 0.1" ) );
 
-	Array<mpdf* > A ( 2 );
-	mepdf A1 ( &f1 );
-	mepdf A2 ( &f2 );
-	A ( 0 ) = &A1;
-	A ( 1 ) = &A2;
+	Array<shared_ptr<mpdf> > A ( 2 );
+	shared_ptr<mepdf> A1 = new mepdf ( f1 );
+	shared_ptr<mepdf> A2 = new mepdf ( f2 );
+	A ( 0 ) = A1;
+	A ( 1 ) = A2;
 
 	int Npoints = 100;
@@ -68,6 +68,6 @@
 	it << Name ( "Res1" ) << Res1;
 	it << Name ( "Res2" ) << Res2;
-	it << Name ( "S1" ) << f1.evallog_m ( Grid );
-	it << Name ( "S2" ) << f2.evallog_m ( Grid );
+	it << Name ( "S1" ) << f1->evallog_m ( Grid );
+	it << Name ( "S2" ) << f2->evallog_m ( Grid );
 	cout << ( ( enorm<ldmat>* ) ( MP->_Coms ( 0 ).get() ) )->_R().to_mat() << endl;
 }
Index: library/tests/merger_test.cpp
===================================================================
--- library/tests/merger_test.cpp (revision 506)
+++ library/tests/merger_test.cpp (revision 507)
@@ -7,8 +7,4 @@
 
 using namespace bdm;
-
-//These lines are needed for use of cout and endl
-using std::cout;
-using std::endl;
 
 TEST ( test_merger_basic ) {
@@ -25,9 +21,7 @@
 	f2->set_parameters ( "5", mat ( "10" ) );
 
-	Array<mpdf* > A ( 2 );
-	mepdf A1 ( f1 );
-	mepdf A2 ( f2 );
-	A ( 0 ) = &A1;
-	A ( 1 ) = &A2;
+	Array<shared_ptr<mpdf> > A ( 2 );
+	A ( 0 ) = new mepdf ( f1 );
+	A ( 1 ) = new mepdf ( f2 );
 
 	int Npoints = 100;
Index: library/tests/testSmp.cpp
===================================================================
--- library/tests/testSmp.cpp (revision 504)
+++ library/tests/testSmp.cpp (revision 507)
@@ -106,9 +106,7 @@
 	eG->set_rv ( y );
 	//create array
-	Array<mpdf*> A ( 2 );
-	mepdf meN ( eN.get() );
-	mepdf meG ( eG.get() );
-	A ( 0 ) = &meN;
-	A ( 1 ) = &meG;
+	Array<shared_ptr<mpdf> > A ( 2 );
+	A ( 0 ) = new mepdf ( eN );
+	A ( 1 ) = new mepdf ( eG );
 
 	mprod eP ( A );
Index: library/tests/epdf_harness.cpp
===================================================================
--- library/tests/epdf_harness.cpp (revision 502)
+++ library/tests/epdf_harness.cpp (revision 507)
@@ -68,9 +68,8 @@
 		shared_ptr<epdf> m = hepdf->marginal ( *mrv );
 		shared_ptr<mpdf> c = hepdf->condition ( crv );
-		mepdf mm ( m );
-
-		Array<mpdf *> aa ( 2 );
-		aa ( 0 ) = c.get();
-		aa ( 1 ) = &mm;
+
+		Array<shared_ptr<mpdf> > aa ( 2 );
+		aa ( 0 ) = c;
+		aa ( 1 ) = new mepdf ( m );
 		mprod mEp ( aa );
 
Index: library/tests/merger_iter_test.cpp
===================================================================
--- library/tests/merger_iter_test.cpp (revision 477)
+++ library/tests/merger_iter_test.cpp (revision 507)
@@ -21,22 +21,19 @@
 	xy.add ( y );
 
-	enorm<fsqmat> f1;
-	f1.set_rv ( xy );
-	enorm<fsqmat> f2;
-	f2.set_rv ( xy );
-	enorm<fsqmat> f3;
-	f3.set_rv ( y );
+	shared_ptr<enorm<fsqmat> > f1 = new enorm<fsqmat>();
+	f1->set_rv ( xy );
+	shared_ptr<enorm<fsqmat> > f2 = new enorm<fsqmat>();
+	f2->set_rv ( xy );
+	shared_ptr<enorm<fsqmat> > f3 = new enorm<fsqmat>();
+	f3->set_rv ( y );
 
-	f1.set_parameters ( "4 3", mat ( "0.4 0.3; 0.3 0.4" ) );
-	f2.set_parameters ( "1 3", mat ( "0.3 -0.2; -0.2 0.3" ) );
-	f3.set_parameters ( "2", mat ( "0.4" ) );
+	f1->set_parameters ( "4 3", mat ( "0.4 0.3; 0.3 0.4" ) );
+	f2->set_parameters ( "1 3", mat ( "0.3 -0.2; -0.2 0.3" ) );
+	f3->set_parameters ( "2", mat ( "0.4" ) );
 
-	Array<mpdf* > A ( 3 );
-	mepdf A1 ( &f1 );
-	mepdf A2 ( &f2 );
-	mepdf A3 ( &f3 );
-	A ( 0 ) = &A1;
-	A ( 1 ) = &A2;
-	A ( 2 ) = &A3;
+	Array<shared_ptr<mpdf> > A ( 3 );
+	A ( 0 ) = new mepdf ( f1 );
+	A ( 1 ) = new mepdf ( f2 );
+	A ( 2 ) = new mepdf ( f3 );
 
 	int Npoints = 100;
