Should be working with non-overlapping intervals

Not sure what happens with overlapping intervals, the result is surely wrong.