Refactor the table selection detection