Better configuration

Adding a new element to configure HA
Simplifying code to avoid race conditions