adding comments and minor refactorin