Fix the bug where some range records were not being emitted.

Added lots of debug info to track this down, but it seems useful in its
own right.
Fix a potential bug, where the global save-file was repoened repeatedly.
Finish a long-standing TODO, and convert SPI_execute() call into SPI_prepare().
Final review for community submission.