Fix #888 by incrementing i only once per iteration