add a test for iterator and simplify the implementation