Updated to support more efficient sparse trees.