LOGI-Mark1 code ported from Pong Chu Book