Fixed thread waiting for ever when no results are retrieved