Added events and handlers.
Refactoring code