Made JavaScript example more robust