Refactor mq-related codes