implemented flatten. not working yet