fixing invalid iterator dereference