Refactored and commented for readability and maintainability. Also added a subscribeAll function.