refactored  to deal with either objects or arrays