fix bug and refactor code
fix stack trace in events