Improve use of iterator functions, remove dead code