clean up.
fix a bug that iterates beyond end()