Second attempt to fix loop