updated code for ch04 and appx D