fix iterator bug and use iterator everywhere