helper functions for lists and trees