Code from N.Outram lecture 23 Oct 2017