Added lots of comments, removed some unused code.