What result do you get instead? What should the algorithm do?

Post an SSCCE that demonstrates the problem, and we'll go from there.