Added loop counter and closed SPI device