Minor cleanup and added more comments.
More cleanup and refactoring.