Added a fair bunch of code from the book