fixed incorrect raising of ValueError
fixed negative indexing check
fixed infinite loop when result set is empty