added fixes for MQTT subscriptions and some refactoring.