Re-org of javascript so it works on Firefox