Move removeEventSourceIfNeeded() to a Utils class so that it can be used from other places.