[bugfix] Invalid results count/memory issue

Rewrite/rebase of #48