update code, fix bugs, and add event handler