Refactored to clean up and remove comments.