refactoring the code to dynamically move across tabs