Refactor tree build into different files.