fix some bugs caused by wrong use of std::find and wrong loop condition