added some comments and changed the conditions under which the number of edges is updated