minor refactoring, added comments