fixed negative indexing check
fixed infinite loop when result set is empty