add comments, optimize parts of code with lambda expressions