Adding some error handling and comments
Fixed, output correct. Added variable level, removed array h_graph_level.