A lot of refactoring and comments.