implementing joining algorithm