Rename tree to snapshot, and improve logic.