added deep flattening functionality