Refactor publishs subscribe code