More than a debug: previous version should have been working in much cases. A better one so. More tests needed. Code review needed too.