moved node handling to within if loop