MQTT Code Re factoring