Modified after lecture 9