updated to faster grid search up until double indexing function