[bugfix] Invalid results count/memory issue

Rewrite/rebase of #48
Merge pull request #51 from ChubV/master

[bugfix] Invalid results count/memory issue