Fixed the overlapping problem and made compatible with downstream functions