Added check for terminal nodes