Revised JS logic

Supports nested tabs, and also prevents some double-request issues.