Refactor and add pubsub