Code for lecture 16 and 17.