refactor and fix issue where event listener throws error on tab updated