fixing bugs found by jsCritic