Commented some of the confusing parts of the code