This has caught a couple of bugs during loop development so far, and I should probably have written it long ago.