sort and handle leaves