Remove recursive call in js, which sometimes laeds to freeze the browser