Added recursion to toArray() and toObject()