Remove MapToTree function to reduce duplication