added leaves and flatten methods