rearrange, add more code for chapter 3