Better looping: write check should be done before iterating, not on every iteration.