correct edge index and avoid doing the same loop twice