move Index query into if.