Refactor tree splitting code to make it re-useable