Added descriptive comments and minor refactorings