Significant refactoring

Rearranged the order of attaching events.
Refactored addEntity/Entities code.
Added entityValue function to allow configuration of how an entity
picker input value is set / calculated.