Partial attempt to refactor tab management code.