added comments, minor refactoring