refactor confusing block + don't search the entire array

Also solves this annoying error which I don't understand, since the code looks equivalent to me.

FATAL: NoMethodError: undefined method `[]' for nil:NilClass