Overall refactoring and more verbose comments.