Cleanup code and add more comments.