Cleanup. Make some things tail recursive.  Add comments.