replaced redundant code by a function
fixed last commit about adding a function to open tab