support querying secondary indexes. Also refactored stuff around