Lots of refactoring and adding of comments.