Fixed several loop errors.