refactor and added pubsub
refactor