further simplified the query and fixed indexes