SPI reimplemented in template form.