refactor
need to use iterator_traits here