tweaking the code of nestObjects to allow for different types of objects (lists of matrices or lists of lists)