slight refactoring, added comments