cleanup and add comments.  Use lapply.  Add functions.