rewrote some of the code to make it more self-documenting and easier for people to read.