add comments and some code cleanup