rewrite loop to read from the array less often
note to self