Solved the problem using fragments that overalp at the ends, but his produces 3 fragments for the second case.
Put everything in a single script