Incorrect read was happening after nodes which are not affect on output
were removed from the graph. Other nodes could have been connected to
this nodes which lead to accessing freed memory in some other places.
Solved by removing links from unused nodes before removing them from
the graph.