Added some checks in search to avoid exceptions