Make clone function more robust