Fix bug in handler attaching logic

Prevent distinct constructors from sharing the same event handlers.
Add TreeMap implementation