Add tests for iterator and fix some bugs