refactoring variables, keeping state of last selected row in main table