Fix bug with switching between leaves