refactor events handling