refactor first conditional in while loop to test if keys[0] is an array