refactoring pubsub code to make it more testable