Improve logic used to determine in which tab URL should be opened, fixes #579