Refactor mq-related code