refactor confusing block + don't search the entire array