Added a title bar to the legend with a button to close the legend
The legend will now close if all layers are removed from the map
Refactored some redundant code