updated code for ch5, 9 and apx D