rearranged the logic in array loop to fix