First working code from lecture note