MQTT clean session implementation, Code refactoring