Refactored destroy of nodes into separate function