Refactor: bind data to events
clean up
reorganize the code