changing the flatten function to handle Array values properly