Slight JavaScript refactoring and adding function comments