add some comments
factor out for loops, add comments