refactor click event logic