function-ized some repeated code (and a few more comments)
removed old code