Working code from lecture 3