Added some checks around query and index