modified  to handle objects and arrays