Adapted spi code.