improved unite substitute function