add code from ch5 to ch8