Added a bunch of comments to (hopefully) explain.