Source code for ch2 & ch3