Link the undelivereds with the delivered.

Also a couple of code cleanup things.