Significant clean-up and added some comments.